Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium/Python - Element not reachable by keyboard

Tags:

selenium

I am trying to fill in a form automatically. I have recorded a script with Selenium.

One of the field to populate is the zip code. When I start typing the code, a new window opens to suggest appropriate option (javascript autofill)

I need to select the first item the ul (cf. html below)

I am quite new to Selenium and though I have been reading the Selenium/html documentation I am totally stuck for almost 1 month on this...

Many thanks in advance for your support

My code is as follows and I received the error message "Element is not reachable by keyboard"

elem = driver.find_element_by_id("location_p")
    elem.send_keys("75")
    first_option = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CLASS_NAME, "selected")))
    first_option.send_keys(Keys.RETURN)

**HTML** 

<div id="localisation_left">


<div class="line toDisable">
    <label for="location_p" class="label">Ville ou code postal *</label>
    <div class="field-wrapper location-container">
        <div class="inputWrapper">
            <i id="browserGeoloc" class="icon-geoloc icon-2x blue"></i>
            <div class="loaderGif-small hidden"></div>

                <input class="nude" name="location_p" id="location_p" autocomplete="off" value="Paris 75010" type="text">

                <input name="zipcode" value="" type="hidden">
                <input name="city" value="" type="hidden">
                <script type="text/javascript">
                    var numberOfLocation = 1, numberOfAuthorizedLocation = 1;
                    var cityNewadMultipleLocation = new MultipleLocationNewad('input[name="location_p"]', numberOfLocation, numberOfAuthorizedLocation);
                    cityNewadMultipleLocation.cityAndZipcodeAreSelected = true;
                </script>

            <input name="region" value="" type="hidden">
            <input name="dpt_code" value="" type="hidden">
        </div>
        <ul class="location-list visible" style="top: 43px;">
<li data-region="12" data-dpt-code="75" class="selected">
                <span class="city" title="Paris">Paris</span>&nbsp;<span class="zipcode">75011</span>
            </li>
                    <li data-region="12" data-dpt-code="75">
                <span class="city" title="Paris">Paris</span>&nbsp;<span class="zipcode">75015</span>
            </li>
                    <li data-region="12" data-dpt-code="75">
                <span class="city" title="Paris">Paris</span>&nbsp;<span class="zipcode">75009</span>
            </li>
                    <li data-region="12" data-dpt-code="75">
                <span class="city" title="Paris">Paris</span>&nbsp;<span class="zipcode">75010</span>
            </li>
                    <li data-region="12" data-dpt-code="75">
                <span class="city" title="Paris">Paris</span>&nbsp;<span class="zipcode">75017</span>
            </li>
like image 767
Olivier Hoen Avatar asked May 19 '18 12:05

Olivier Hoen


People also ask

Is not reachable by keyboard?

Element is not reachable by keyboard in plain words means that the element can't be reached using the keyboard, which means you won't physically interact with it even. There are different approaches to address this issue.

How would you click on an element which is not on the visible part of the screen?

We can click on an element which is hidden with Selenium webdriver. The hidden elements are the ones which are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".

How do you press and hold a key in Selenium?

To hold down a key simultaneously while another key is being pressed, we use the keyDown() and keyUp() methods. Both these methods accept the modifier key as a parameter. The action of these two methods on a key yields a special functionality of a key. All these methods are a part of Actions class in Selenium.

Which command allows Selenium to enter the enter key in a input box?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method.


4 Answers

You can click on the first option, instead of pressing Enter key

elem = driver.find_element_by_id("location_p")
elem.send_keys("75")

condition = EC.visibility_of_element_located((By.CSS, 
    "label[for='location_p'] + div ul.location-list > li"))

first_option = WebDriverWait(driver, 15).until(condition)

first_option.click()
like image 69
yong Avatar answered Jan 03 '23 23:01

yong


I had a similar issue, and the above solution did not work for me (it would throw an invalid syntax error).

  1. I first used the find_element_by_css_selector function, which selects the first occurrence of the element with given attributes. This did not work.

  2. Then I used the find_elements_by_css_selector (notice the s), which returns a list of the elements with given attributes. There were two elements in that list. Of course the first one (with index [0]) was not accessible by keyboard: this is equivalent of doing (1) above. But the second element (with index [1]) was accessible by keyboard.

Problem solved.

like image 30
Madpentiste Avatar answered Jan 04 '23 00:01

Madpentiste


Try selecting by using Xpath below

elem = driver.find_element_by_id("location_p") elem.send_keys("75")  
first_option = WebDriverWait(driver, 10).until( 
              EC.visibility_of_element_located((By.Xpath, 
              ".//*[@id='localisation_left']/div/div/ul/li[1]"))) 
first_option.click()
like image 27
Prany Avatar answered Jan 04 '23 00:01

Prany


If anyone faces Element not reachable by keyboard issue, one can also seek below approach:

input_xpath = '//input[@type="file"][@name="files[]"][@class="class_name"]'
input_element = self.driver.find_element_by_xpath(input_xpath)

## to make element visible:
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
                                       input_element)
input_element.send_keys('~\Desktop\Release2.pdf')
like image 23
Pranjay Kaparuwan Avatar answered Jan 04 '23 01:01

Pranjay Kaparuwan