【问题标题】:ElementNotVisibleException when Selenium WebDriver isn't full screen当 Selenium WebDriver 未全屏时发生 ElementNotVisibleException
【发布时间】:2016-03-21 13:43:06
【问题描述】:

我正在尝试让带有 Selenium WebDriver 的 Firefox 在此 Boerse Frankfurt webpage 的搜索字段中输入搜索查询。

我可以通过find_element_by_namefind_element_by_xpath 成功定位web 元素,得到<selenium.webdriver.remote.webelement.WebElement object at 0x10768e490>

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

ElementNotVisibleException:消息:元素当前不可见 因此可能无法与之交互

我以前在使用 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)

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

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    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)
    

    【讨论】:

    • 它等待元素的可见性,直到出现TimeoutException。对于肉眼,该元素在浏览器中是可见的。也许有一个“隐藏”元素之类的。
    • @Winterflags 这很奇怪,它对我有用。如果您在导航到页面之前最大化浏览器窗口:driver.maximize_window(),会怎样?
    • 解决了!好发现!我实际上已将驱动程序设置为仅使用屏幕的左半部分。我自己可能永远不会想到这一点。
    【解决方案2】:
    driver.get("http://en.boerse-frankfurt.de/")
    //make some wait here to fully load the browser.
    
    driver.find_element_by_xpath(".//*[@id='searchvalue']").click()
    search_box.send_keys(search_string)
    

    首先点击搜索框,然后点击发送键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      相关资源
      最近更新 更多