Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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/3/html/83.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
Selenium(PYTHON)检查元素是否存在_Python_Html_Selenium_Webautomation - Fatal编程技术网

Selenium(PYTHON)检查元素是否存在

Selenium(PYTHON)检查元素是否存在,python,html,selenium,webautomation,Python,Html,Selenium,Webautomation,因此,我试图找出如何正确运行这个循环,我的问题是,根据加载的链接,加载的页面将出现访问被拒绝错误,这与所有链接不同,我的问题是,我想确定某个特定元素是否加载到我的屏幕上,程序识别它并中断循环,并在for循环中开始下一次迭代,因此我试图确定是否存在“拒绝访问”元素,如果存在,则中断,否则,继续for循环 idList = ["8573", "85678", "2378", "2579"] for ID in idL

因此,我试图找出如何正确运行这个循环,我的问题是,根据加载的链接,加载的页面将出现访问被拒绝错误,这与所有链接不同,我的问题是,我想确定某个特定元素是否加载到我的屏幕上,程序识别它并中断循环,并在for循环中开始下一次迭代,因此我试图确定是否存在“拒绝访问”元素,如果存在,则中断,否则,继续for循环

idList = ["8573", "85678", "2378", "2579"]


for ID in idList:


    print(ID)
    driver.get(f"https://www.someWebsite/username/{ID}")

    element = driver.find_element_by_class_name("Access-Denied")
   
    print("error loading website")
    break
if not element:

    print("you may continue the for loop")

请注意,如果显示拒绝访问页面的元素不存在,我会收到一个错误,即“拒绝访问”元素不存在,我如何修复此错误?

您希望等待网页收到正确的响应。使用以下代码,您可以等待加载的完整响应,然后执行以下操作 根据结果采取适当行动:

from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

...
try:
    _ = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "Access-Denied"))
        )
    print("error loading website")
    break
except TimeoutException:
    print("you may continue the for loop")
...


因此,如果存在拒绝访问的情况,则需要循环,然后中断

wait = WebDriverWait(driver, 10)
idList = ["8573", "85678", "2378", "2579"]

for ID in idList:
    print(ID)
    driver.get(f"https://www.someWebsite/username/{ID}")
    try:
        element=wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'Access-Denied')))
        break
    except:
        continue
进口

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