gpt4 book ai didi

python - 如何在 Python 中使用 Selenium 从具有 "display: none"属性的 Web 元素中选择任何元素

转载 作者:太空狗 更新时间:2023-10-30 02:10:47 26 4
gpt4 key购买 nike

我需要从带有 display: none 属性的 web 元素中选择任何元素,如下所示:

<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>

我可以通过网络浏览器手动完成,但我需要通过 Python 中的 Selenium 完成。不幸的是,当我有以下代码时:

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 抛出异常并显示以下信息:

Message: ''

异常的类型是selenium.common.exceptions.TimeoutException

如何实现这个元素的交互?在这种情况下如何选择任何元素?

提前致谢。

最佳答案

使用 execute_script() 设置该元素的显示属性,然后使用 Selenium Select 选择所需的值。

下面的代码应该适合你:

    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)

关于python - 如何在 Python 中使用 Selenium 从具有 "display: none"属性的 Web 元素中选择任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com