Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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是一个隐藏的框架,有时与java脚本断言一起出现_Python_Selenium_Hidden_Execute Script - Fatal编程技术网

Python Selenium是一个隐藏的框架,有时与java脚本断言一起出现

Python Selenium是一个隐藏的框架,有时与java脚本断言一起出现,python,selenium,hidden,execute-script,Python,Selenium,Hidden,Execute Script,我有一个通知,出现在一个星期几次在这个网站上我刮。我无法逃避 我可以运行代码 el = driver.find_element_by_xpath("//input[@id='btnRead']") driver.execute_script("arguments[0].click();", el) 这会清除它,但如果我把它留在代码中,它会给我一个没有这样的元素异常。事件,如果我尝试像这样用try/catch来包装它 from selenium.common.exceptions import

我有一个通知,出现在一个星期几次在这个网站上我刮。我无法逃避

我可以运行代码

el =  driver.find_element_by_xpath("//input[@id='btnRead']")
driver.execute_script("arguments[0].click();", el)
这会清除它,但如果我把它留在代码中,它会给我一个没有这样的元素异常。事件,如果我尝试像这样用try/catch来包装它

from selenium.common.exceptions import NoSuchElementException

try:
    el = driver.find_element_by_xpath("//input[@id='btnRead']")
    driver.execute_script("arguments[0].click();", el)
except NoSuchElementException:
    print(nonefound)
sleep(5)
driver.quit()
这也会清除它,如果它存在,但如果它不存在,则返回错误。 我假设我做错了什么,但我尝试了几个不同的版本,我总是得到错误,使窗口挂起,并停止执行脚本的其余部分


任何想法都很好。

如果要继续脚本,可以检查元素的长度

若元素的长度大于0,则会单击

if len(driver.find_elements_by_xpath("//input[@id='btnRead']"))>0 :
    el = driver.find_element_by_xpath("//input[@id='btnRead']")
    driver.execute_script("arguments[0].click();", el)
else:
    print("nonefound")

或者诱导
WebDriverWait
()和
位于的元素的可见性
()

您需要导入以下库

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

是否可能存在除
NoTouchElementException
之外的其他异常。“挂起”可能来自
TimeoutException
。尝试在那里打印异常,如下所示:

from selenium.common.exceptions import NoSuchElementException

try:
    el = driver.find_element_by_xpath("//input[@id='btnRead']")
    driver.execute_script("arguments[0].click();", el)
except Exception as e:
    print(e)
sleep(5)
driver.quit()

这将是你的答案。请试一试。我试过了,但没法用。我感谢你的帮助。为了澄清,我可以在元素存在时找到它。我就是抓不住这个错误,无法阻止代码在没有被破坏的情况下被破坏。非常感谢,我以前尝试过这个版本,一定是在什么地方把代码弄乱了。这完全解决了我的问题。我只是用10种不同的方法测试了它。
from selenium.common.exceptions import NoSuchElementException

try:
    el = driver.find_element_by_xpath("//input[@id='btnRead']")
    driver.execute_script("arguments[0].click();", el)
except Exception as e:
    print(e)
sleep(5)
driver.quit()