gpt4 book ai didi

python - 当 Selenium WebDriver 未全屏时出现 ElementNotVisibleException

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:05 26 4
gpt4 key购买 nike

我正在尝试使用 Selenium WebDriver 让 Firefox 在 Boerse Frankfurt webpage 的搜索字段中输入搜索查询.

我可以通过 find_element_by_name 成功找到 Web 元素或find_element_by_xpath ,给出 <selenium.webdriver.remote.webelement.WebElement object at 0x10768e490> .

但是,当尝试清除字段、发送 key 或以其他方式单击它时,我收到错误消息:

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

自从之前使用 Selenium 时我就不熟悉这个错误,所以我不知道我的代码可能有什么问题:

driver.get("http://en.boerse-frankfurt.de/")

search_string = "test"

search_box = driver.find_element_by_xpath(".//*[@id='searchvalue']")
search_box.send_keys(search_string)
search_box.send_keys(Keys.RETURN)
<小时/>

编辑:这里的问题实际上是驱动程序窗口没有最大化到全屏。请参阅下面的答案/评论。

最佳答案

Wait让搜索字段变得可见,然后才与其交互:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("http://en.boerse-frankfurt.de/")

search_string = "test"

wait = WebDriverWait(driver, 10)
search_box = wait.until(EC.visibility_of_element_located((By.ID, "searchvalue")))

search_box.clear()
search_box.send_keys(search_string)
search_box.send_keys(Keys.RETURN)

关于python - 当 Selenium WebDriver 未全屏时出现 ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36132658/

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