python - selenium.common.exceptions.ElementNotVisibleException : Message: element not visible while invoking send_keys in ubuntu headless browser through python

标签 python selenium selenium-webdriver xpath css-selectors

我在使用 send_keys 时遇到问题

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path= '/home/ec2-user/chromedriver', chrome_options=options)
base_url = "https://www.xxxxxxxx.com/"
driver.get(base_url)
driver.find_elements_by_xpath("//input[@id = 'homepage_search_box']")[0].send_keys("Pink's Hot Dogs")

上面的代码在 Windows 中完美运行,但是当涉及到 ubuntu 时,它会抛出类似的错误

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: headless chrome=67.0.3396.99)
  (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.14.47-56.37.amzn1.x86_64 x86_64)

HTML:

<form class="form-inline" onsubmit="simple_search(jQuery('#homepage_search_box').val());return false;">
    <div class="form-group col-md-8">
        <label class="sr-only" for="homepage_search_box">Company Name or Report #</label>
        <input name="homepage_search_box" id="homepage_search_box" class="form-control" placeholder="Company Name or Report #" type="text">
    </div>
    <div class="form-group col-md-4">
        <button type="submit" class="btn btn-default">Search</button>
    </div>
</form>

谁能帮忙

driver.find_elements_by_xpath("//input[@id = 'homepage_search_box']")[0]

它完美地给出了 selenium 对象 我在发送 key 时遇到错误

最佳答案

要将字符序列发送到搜索框,您需要引发WebDriverwait以使所需的元素可点击,并且您可以使用以下任一方法解决办法如下:

  • 代码块:

    # -*- coding: UTF-8 -*-
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    options.add_argument('--headless')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.ripoffreport.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.header-field#header-search-text"))).send_keys("Pink's Hot Dogs")
    # WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='header-field'][@id='header-search-text']"))).send_keys("Pink's Hot Dogs")
    print("Search Text Sent")
    
  • 控制台输出:

    Search Text Sent
    

关于python - selenium.common.exceptions.ElementNotVisibleException : Message: element not visible while invoking send_keys in ubuntu headless browser through python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51263444/

相关文章:

selenium-webdriver - 如何确认我使用的是正确的 chromedriver?

java - 如何处理 selenium webdriver 中的弹出窗口(不警报)?

python - 如何创建在图像中仅显示黑色的白色蒙版?

python - 在数据框中标记用户的出席情况

Python-迭代列表时如何处理异常?

python - css first 和 last 在 selenium rc 中不起作用

java - 使用 Java 的 Selenium WebDriver 和 HTML 窗口位置

python - 如何在不使用 .remove 的情况下从列表中删除元素

python - 如何使用 virtualenv 在 Aptana Studio 3 上进行 PyDev 代码分析?

testing - 使用 selenium webdriver 进行表单初始化