如何使用“显示:无"从web元素中选择任何元素.在Python中使用Selenium属性 [英] How to select any element from the web element with "display: none" attribute using Selenium in Python
问题描述
我需要从Web元素中选择具有display: none
属性的任何元素,如下所示:
I need to select any element from the web element with display: none
attribute that looks like this:
<div class="some_class">
<select id="some_id" class="some_select_class" style="display: none;">
<option value="1" data-isleaf="false" data-catid="3" data-special_message="" data-adtypeid="0">1</option>
<option value="2" data-isleaf="true" data-catid="4" data-special_message="" data-adtypeid="1">2</option>
</select>
</div>
我可以从Web浏览器中手动完成此操作,但是我需要通过Python中的Selenium来完成.不幸的是,当我有以下代码时:
I can do it manually from a web browser, but I need to do it via Selenium in Python. Unfortunately, when I have the following code:
try:
element = selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_id')))
selenium.webdriver.support.ui.Select(element).select_by_value('1')
except Exception as ex:
print(ex)
WebDriverWait
引发带有以下信息的异常:
WebDriverWait
throws an exception with the following info:
消息:"
例外的类型是selenium.common.exceptions.TimeoutException
如何实现此元素的交互?在这种情况下如何选择任何元素?
How can I achieve the interaction of this element? How can I select any element in this case?
谢谢.
推荐答案
使用execute_script()
设置该元素的显示属性,然后使用Selenium Select
选择所需的值.
Use execute_script()
to set the display property of that element and then use the Selenium Select
for selecting a required value.
下面的代码应该对您有用:
Below code should work for you:
try:
selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_other_id_on_page')))
selenium.execute_script("document.getElementById('some_id').style.display='inline-block';")
element = selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_id')))
selenium.webdriver.support.ui.Select(element).select_by_value('1')
except Exception as ex:
print(ex)
这篇关于如何使用“显示:无"从web元素中选择任何元素.在Python中使用Selenium属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!