硒不能点击隐藏的元素

2024-03-28 22:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium和Python。我正在努力使用click()方法来单击动态创建的单选按钮。收音机的标记如下。在

<input version="2" value="1" class="linked-ftb-radio meta(controlNumber=2)" id="radio_1" name="IndexString" reference="TEST 01" type="radio">
<label for="radio_1" id="linked-label" class="radio-label"></label>

我的密码是:

^{pr2}$

但是会产生以下错误:

Traceback (most recent call last):
  File "index.py", line 41, in <module>
    driver.find_element_by_xpath('//*[@id="radio_1"]').click()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 74, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

当按下标签时,通过改变图像来模拟收音机。换句话说,当单击时,类将更改为radio-label selected。在

radio

考虑到目前还看不到Selenium,如何单击Selenium的单选按钮?在


Tags: inpylibpackagesseleniumlinelibrarysite
2条回答

尝试explicit condition等待元素显示。(如果是与时间相关的问题,即需要时间来显示)

 element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, "//*[@id='radio_1']"))
 element.click()

This waits up to 10 seconds before throwing a TimeoutException or if it is present on the DOM of a page and visible, will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

参考文献:

  1. https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.visibility_of_element_located
  2. http://selenium-python.readthedocs.io/waits.html

C#

您可以使用waiter to element使其变得可见,如下所示:

var element = Waiter.Until(ExpectedConditions.ElementIsVisible(By.Id("ID"))).FirstOrDefault();

相关问题 更多 >