Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 检查元素是否存在_Python_Selenium_Selenium Webdriver_Webdriver_Ui Automation - Fatal编程技术网

Python 检查元素是否存在

Python 检查元素是否存在,python,selenium,selenium-webdriver,webdriver,ui-automation,Python,Selenium,Selenium Webdriver,Webdriver,Ui Automation,我正试图通过以下方式定位元素: element=driver.find_element_by_partial_link_text("text") 在Python中,selenium和元素并不总是存在。是否有一个快速行来检查它是否存在,并在错误消息不存在时得到NULL或FALSE 您可以执行尝试/,但块除外,如下检查元素是否存在: from selenium.common.exceptions import NoSuchElementException try: element=dri

我正试图通过以下方式定位元素:

element=driver.find_element_by_partial_link_text("text")

在Python中,selenium和元素并不总是存在。是否有一个快速行来检查它是否存在,并在错误消息不存在时得到NULL或FALSE

您可以执行
尝试
/
,但
块除外,如下检查元素是否存在:

from selenium.common.exceptions import NoSuchElementException

try:
    element=driver.find_element_by_partial_link_text("text")
except NoSuchElementException:
    print("No element found")
或者使用
find_elements_uu…()
方法中的一种检查相同的内容。它应该返回空列表或由传递的选择器匹配的元素列表,但如果未找到元素,则不会出现异常:

elements=driver.find_elements_by_partial_link_text("text")
if not elements:
    print("No element found")  
else:
    element = elements[0]  

有时元素不会立即出现,在这种情况下,我们需要使用显式等待:

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 5)

def is_element_exist(text):
    try:
        wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, text)))
    except TimeoutException:
        return False
不带
的解决方案尝试/除外

def is_element_exist(text):
    elements = wait.until(EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, text)))
    return None if elements else False
您可以读取显式等待的工作方式

进口:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

您所说的快速检查语句是什么意思?Selenium不支持
语句
。您必须编写一行/一块代码。
驱动程序。通过部分链接文本(“文本”)查找元素。
是检查元素是否存在的足够快的方法。。。您能更明确地说明您希望代码做什么吗?当元素不存在时,它会给出错误消息。当它不存在时,是否可能获取NULL或FALSE?