Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当xpath在使用selenium和python的页面上不可用时不引发异常?_Python_Selenium_Xpath - Fatal编程技术网

当xpath在使用selenium和python的页面上不可用时不引发异常?

当xpath在使用selenium和python的页面上不可用时不引发异常?,python,selenium,xpath,Python,Selenium,Xpath,如果xpath在页面上不可用,那么它应该抛出异常,但它会导致整个程序出错 我已经使用selenium加载了完整的页面,然后我尝试单击所有的“阅读更多”链接,但不起作用 页面链接 示例代码 xpath_content=".//*[@id='reviews-container']/div[1]/div[3]/div[1]/div/div/div[3]/div/div[1]/div[2]/div/span" temp="true" while temp: try: WebDr

如果xpath在页面上不可用,那么它应该抛出异常,但它会导致整个程序出错

我已经使用selenium加载了完整的页面,然后我尝试单击所有的“阅读更多”链接,但不起作用

页面链接

示例代码

xpath_content=".//*[@id='reviews-container']/div[1]/div[3]/div[1]/div/div/div[3]/div/div[1]/div[2]/div/span"
temp="true"
while temp:
    try:
        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,xpath_content))) 
        urls=driver.find_element_by_xpath(xpath_content)
        text=urls.text
        if text:
            temp=text  
        print "XPATH read_more=",temp
        driver.find_element_by_xpath(xpath_content).click()
    except TimeoutException:
        print driver.title, "no Read more review"
        temp=""
xpath_content=".//*[@id='reviews-container']/div[1]/div[3]/div[1]/div/div/div[3]/div/div[1]/div[2]/div/span" 
try:
     WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,xpath_content))) 
    driver.execute_script('$("span.read-more").click();') 
except TimeoutException:
    print driver.title, "no Read more review"
出现错误

 File "zomato_delhi.py", line 31, in read_more_content
driver.find_element_by_xpath(xpath_content).click()
 File "/usr/local/lib/python2.7/dist- packages/selenium/webdriver/remote/webelement.py", line 65, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python2.7/dist- packages/selenium/webdriver/remote/webelement.py", line 402, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command-processor.js:9587)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command-processor.js:12257)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)
文件“zomato\u drih.py”,第31行,阅读更多内容
驱动程序。通过xpath(xpath内容)查找元素。单击()
文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py”,第65行,单击
self.\u执行(命令。单击\u元素)
文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py”,第402行,在
返回self.\u parent.execute(命令,参数)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/webdriver.py”,执行中的第175行
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python2.7/dist packages/selenium/webdriver/remote/errorhandler.py”,第166行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互
堆栈跟踪:
在fxdriver.premissions.visible(file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command processor.js:9587)
在DelayedCommand.prototype.CheckPremissions\u(file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command processor.js:12257)
在DelayedCommand.prototype.executeInternal\uh处(file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command processor.js:12274)
在DelayedCommand.prototype.executeInternal\u(file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command processor.js:12279)
在DelayedCommand.prototype.execute/<(file:///tmp/tmpKtHaW3/extensions/fxdriver@googlecode.com/components/command processor.js:12221)
删除while循环


它在删除While循环后工作

我只能对XPath表达式进行注释,而不能对Selenium代码进行注释。你的表情太复杂了,为什么不干脆用

//span[@class = "read-more"]
确定正确的跨度元素?例如,在Firefox或Chrome的Web控制台中,使用

$x('//span[@class = "read-more"]')
你可以试着替换

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, xpath_content))) 


这对我很有效

xpath_content=".//*[@id='reviews-container']/div[1]/div[3]/div[1]/div/div/div[3]/div/div[1]/div[2]/div/span"
temp="true"
while temp:
    try:
        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,xpath_content))) 
        urls=driver.find_element_by_xpath(xpath_content)
        text=urls.text
        if text:
            temp=text  
        print "XPATH read_more=",temp
        driver.find_element_by_xpath(xpath_content).click()
    except TimeoutException:
        print driver.title, "no Read more review"
        temp=""
xpath_content=".//*[@id='reviews-container']/div[1]/div[3]/div[1]/div/div/div[3]/div/div[1]/div[2]/div/span" 
try:
     WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,xpath_content))) 
    driver.execute_script('$("span.read-more").click();') 
except TimeoutException:
    print driver.title, "no Read more review"
不需要任何循环

driver.execute_脚本('$(“span.read more”)。单击();')


它将单击页面上的所有“阅读更多”链接。

否,这不会改变任何内容。它不会给我任何错误,但没有单击页面上的所有“阅读更多”链接page@user3902208:可能是因为xpath表达式中的错误?但问题是错误,不是吗?