Python 和 Selenium 以“execute_script”解决“ElementNotVisibleException”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31397462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python and Selenium To “execute_script” to solve “ElementNotVisibleException”
提问by Mark K
I am using Selenium to save a webpage. The content of webpage will change once certain checkbox(s) are clicked. What I want is to click a checkbox then save the page content. (The checkboxes are controlled by JavaScript.)
我正在使用 Selenium 来保存网页。单击某些复选框后,网页内容将发生变化。我想要的是单击复选框然后保存页面内容。(复选框由 JavaScript 控制。)
Firstly I used:
首先我使用了:
driver.find_element_by_name("keywords_here").click()
it ends with an error:
它以错误结束:
NoSuchElementException
then I tried “xpath” like, with implicit/explicit waiting:
然后我尝试了“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()
it gives an error:
它给出了一个错误:
ElementNotVisibleException
Posts
帖子
How to force Selenium WebDriver to click on element which is not currently visible?
如何强制 Selenium WebDriver 单击当前不可见的元素?
Selenium Element not visible exception
suggest it should make the checkboxes visible before clicking, for example using:
建议它应该在单击之前使复选框可见,例如使用:
execute_script
The question may sounds stupid, but how can I find out the proper sentence to “execute_script” the visibility of checkbox from the page source code?
这个问题可能听起来很愚蠢,但是我怎样才能从页面源代码中找到“execute_script”复选框可见性的正确句子?
Besides that, is there another way?
除此之外,还有别的方法吗?
Thanks.
谢谢。
by the way, the line html code looks like:
顺便说一句,该行 html 代码如下所示:
<input type="checkbox" onclick="ComponentArt_HandleCheck(this,'p3',11);" name="keywords_here">
its xpath looks like:
它的 xpath 看起来像:
//*[@id="TreeView1_item_11"]/tbody/tr/td[3]/input
采纳答案by alecxe
Alternative option would be to make the click()
inside execute_script()
:
另一种选择是制作click()
内部execute_script()
:
# 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)
where EC
is imported as:
其中EC
导入为:
from selenium.webdriver.support import expected_conditions as EC
Alternatively and as an another shot in the dark, you can use the element_to_be_clickable
Expected Condition and perform the click in a usual way:
或者,作为在黑暗中的另一个镜头,您可以使用element_to_be_clickable
预期条件并以通常的方式执行点击:
wait = WebDriverWait(driver, 10)
checkbox = wait.until(EC.element_to_be_clickable((By.NAME, "keywords_here")))
checkbox.click()
回答by Roskoe
I had some issues with expected conditions, I prefer building my own timeout.
我对预期条件有一些问题,我更喜欢建立自己的超时。
import time
from selenium import webdriver
from selenium.common.exceptions import \
NoSuchElementException, \
WebDriverException
from selenium.webdriver.common.by import By
b = webdriver.Firefox()
url = 'the url'
b.get(url)
locator_type = By.XPATH
locator = "//label[contains(text(),' keywords_here')]/../input[@type='checkbox']"
timeout = 10
success = False
wait_until = time.time() + timeout
while wait_until < time.time():
try:
element = b.find_element(locator_type, locator)
assert element.is_displayed()
assert element.is_enabled()
element.click()
success = True
break
except (NoSuchElementException, AssertionError, WebDriverException):
pass
if not success:
error_message = 'Failed to click the thing!'
print(error_message)
might want to add InvalidElementStateException, and StaleElementReferenceException
可能想要添加 InvalidElementStateException 和 StaleElementReferenceException