Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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

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
Python Selenium如果在屏幕上定位,则无法定位元素_Python_Selenium_If Statement_Error Handling - Fatal编程技术网

Python Selenium如果在屏幕上定位,则无法定位元素

Python Selenium如果在屏幕上定位,则无法定位元素,python,selenium,if-statement,error-handling,Python,Selenium,If Statement,Error Handling,我会简单地回答这个问题: 我有一个if声明: if self.driver.find_element_by_xpath('//*[contains(@id, "mission_countdown")]'): 我得到了这个错误: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//

我会简单地回答这个问题: 我有一个if声明:

if self.driver.find_element_by_xpath('//*[contains(@id, "mission_countdown")]'):
我得到了这个错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id, "mission_countdown")]"}
为什么我会得到这个?我的意思是这是一个if语句,如果它出现在屏幕上


感谢您的帮助

通过xpath查找元素
可以返回

  • WebElement
    (如果找到)
  • NoTouchElementException
    (如果未找到)
如果您正在搜索可计算为布尔值的输出以在
If
/
else
块中使用,请尝试
查找元素

if self.driver.find_elements_by_xpath('//*[contains(@id, "mission_countdown")]'):
    print('At least one element was found')
else:
    print('No elements found')

下面的代码是一个示例。 它显式等待并移动到要查找的对象元素。如果在try中添加未找到元素,则会抛出except错误消息

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#--------
# all other code to begin scraping here
#--------

try:
    if self.driver.find_element_by_xpath('//*[contains(@id, "mission_countdown")]'):
       print("found this button")

    elif: 
        print("not found trying new button ")
        self.driver.find_element_by_xpath("xpath here")

except:
    print("ERROR MESSAGE: NO ELEMENT FOUND")

我希望这有助于使用
try…catch
block。如果不是他们的,它将转到catch block。请告诉我这是否适合您

try:
    if self.driver.find_element_by_xpath('//*[contains(@id, "mission_countdown")]'):
        print('found')
except:
    print('Not found')

如果它在屏幕上,执行您定义的任何内容,如果不是,您使用的代码是什么?您是否定义了else子句来检查它是否在屏幕上?如果它不在屏幕上,他只需打印hi,不,我没有else子句来检查它是否不在屏幕上也许你应该添加else子句,因为如果它不在屏幕上,它会崩溃,因为没有检查的条件我添加了一个elif not in screen not,它仍然在第一个if上卡住,如果它在屏幕上一切正常,但如果它不在屏幕上,我不想等到它出现,因为它并不总是在那里。我希望它点击一个特定的按钮,如果它在那里,并在一个不同的,如果它不是。。。但是thanksok如果你不想等待,那么只需使用try:except和if-elif。我已经做了编辑,这也不起作用,但我有解决办法。。。我只需要写find_element S Thanks,我真的不知道为什么,因为屏幕上只有一个元素,但它是有效的:)@LaurenzZ不像
find_element_by_xpath
find_elements_by_xpath
返回WebElements列表或空列表
[]
。这两种情况下的输出都可以作为布尔值计算:非空列表始终为
True
,空列表始终为
False
,因此您可以在
if
/
else
中使用它-它永远不会导致exception@LaurenzZ如果你不明白这一点,你应该多读几遍,直到你明白为止。这是非常重要的信息,如果您打算继续编写Selenium代码,它将对您非常有用。