作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Selenium 保存网页。单击某些复选框后,网页内容将发生变化。我想要的是单击一个复选框,然后保存页面内容。 (复选框由 JavaScript 控制。)
首先我使用了:
driver.find_element_by_name("keywords_here").click()
它以错误结束:
NoSuchElementException
然后我尝试了类似“xpath”的方式,隐式/显式等待:
URL = “the url”
verificationErrors = []
accept_next_alert = True
aaa = driver.get(URL)
driver.maximize_window()
WebDriverWait(driver, 10)
#driver.find_element_by_xpath(".//*[contains(text(), ' keywords_here')]").click()
#Or:
driver.find_element_by_xpath("//label[contains(text(),' keywords_here')]/../input[@type='checkbox']").click()
它给出了一个错误:
ElementNotVisibleException
帖子
How to force Selenium WebDriver to click on element which is not currently visible?
Selenium Element not visible exception
建议它应该在单击之前使复选框可见,例如使用:
execute_script
这个问题听起来很愚蠢,但是我如何从页面源代码中找出正确的语句来“execute_script”复选框的可见性?
除此之外,还有别的方法吗?
谢谢。
顺便说一下,这行 html 代码如下所示:
<input type="checkbox" onclick="ComponentArt_HandleCheck(this,'p3',11);" name="keywords_here">
它的 xpath 看起来像:
//*[@id="TreeView1_item_11"]/tbody/tr/td[3]/input
最佳答案
另一种选择是在 execute_script()
中创建 click()
:
# wait for element to become present
wait = WebDriverWait(driver, 10)
checkbox = wait.until(EC.presence_of_element_located((By.NAME, "keywords_here")))
driver.execute_script("arguments[0].click();", checkbox)
其中 EC
被导入为:
from selenium.webdriver.support import expected_conditions as EC
或者,作为黑暗中的另一个镜头,您可以使用 element_to_be_clickable
预期条件并以通常的方式执行点击:
wait = WebDriverWait(driver, 10)
checkbox = wait.until(EC.element_to_be_clickable((By.NAME, "keywords_here")))
checkbox.click()
关于javascript - Python和Selenium To “execute_script” 解决 “ElementNotVisibleException”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31397462/
我是一名优秀的程序员,十分优秀!