venkatesh
venkatesh

Reputation: 141

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

I am having the trouble with the 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")

the above code working perfectly in windows but when it comes to ubuntu it trowing an error like

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>

can any one help

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

it is giving selenium object perfectly i am getting error at sending keys

Upvotes: 1

Views: 743

Answers (3)

Ankit Gupta
Ankit Gupta

Reputation: 786

you can try with javaScript

        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].click();", webElement);
        executor.executeScript("arguments[0].setAttribute('value', '"+text which you want to enter+"');",webElement);

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193208

To send character sequence to the search box you need to induce WebDriverwait for the desired element to be clickable and you can use either of the following solutions:

  • Code Block:

    # -*- 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")
    
  • Console Output:

    Search Text Sent
    

Upvotes: 1

nickyfot
nickyfot

Reputation: 2019

Cannot reproduce withought the actual url, but you can try a few things:

  • sleep a couple of seconds before sending keys (the page might not be loaded)
  • it might be worth trying Firefox/geckodriver instead of Chrome; I've had issues before with Chrome as well which do not seem to happen on Firefox.
  • Moreover, if you have an adblocker on Chrome when copying x_path etc it can sometimes change the page structure

Hope this helps!

Upvotes: 0

Related Questions