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

标签 python selenium selenium-webdriver

我正在尝试使用 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/

相关文章:

python - 将所有标准输出存储到文件中,同时仍将其显示在屏幕上

java - 从 Selenium 的隐藏下拉菜单中选择一个选项

scala - 为 Play 项目并行运行 Selenium 测试

python - 使用 selenium、bs4 或 requests 从交互式图表中抓取数据

javascript - 我怎样才能通过 selenium 自动化测试我的 json web 服务?

python - 从 Selenium 中的 url 下载 CSV?

python - 如何在 python 中插入地理引用数据?

java - 破折号和参数是否有任何标准的命令行约定?

python - 尝试使用 Anaconda 启动 Spyder 时出错

c# - 如何在 selenium webdriver 中使用等待