0

i am working on getting data from a website and any time i click on a link to get phone number, a modal form is opened to ask for my phone number first and the phone number is shown to me.

The challenge i have now is, i want to send keys to the input field, so because the input field has no name or id, i located the input field using xpath:

xxx = driver.find_element_by_xpath("//input[@placeholder='081xxxxxxxx']")

I printed this and it returned some selenium objects, but when i try sending keys like:

xxx.send_keys('08100000000')

Here is a snippet of the html code:

<div class="row">
<div class="medium-12 columns">
    <div class="guest-text">
        One step closer! <br />
        <span>Please provide your contact number to view business contact details</span>
    </div>

    <form action="" data-abide="ajax" novalidate="novalidate">
        <div id="txtUserPhoneNumber" ng-show="!isLoggedIn && collectUserPhone == ''" class="guest-no">
            <label>
                <div class="guest-label">Phone Number</div>
                <input type="text" placeholder="081xxxxxxxx" ng-model="UserPhoneNew" required data-invalid="" aria-invalid="true" maxlength="11">

            </label>
        </div>
    </form>
</div>

I have tried:

xxx = driver.find_element_by_xpath("//input[@placeholder='081xxxxxxxx']")
xxx.send_keys('08100000000')

dummy_number = driver.find_element_by_xpath("//div[contains(@class, 'modal small guest')]/div[contains(@class, 'guest-modal-wrapper')]//form[1]//input[1]")
dummy_number.send_keys('081000000')

I got error saying:

Traceback (most recent call last):
File "Dropbox/automation/vconnect.py", line 76, in <module>
    RunAutomation.instantiatechrome()
  File "Dropbox/automation/vconnect.py", line 61, in instantiatechrome
    xxx.send_keys('081xxxxxxxx')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=66.0.3359.139)
  (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.13.0-39-generic x86_64)

Your opinion is welcomed.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
molecules
  • 21
  • 2
  • 12

3 Answers3

1

The error messgae says the element is not visible. So you have to explicitly wait until it becomes visible. Use an ExpectedCondition as described here.

In the end your code might look like this (waiting up to 120 seconds):

from selenium.webdriver.support import expected_conditions as EC

...

wait = WebDriverWait(driver, 120)
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='081xxxxxxxx']")))
element.send_keys('08100000000')
Würgspaß
  • 4,660
  • 2
  • 25
  • 41
0

About the error, you need to wait the element be displayed and enabled to interact with. You can learn it reading about "Implicity and Explicity waits in Selenium".

Here you can find some extra info abot that: https://stackoverflow.com/a/27600986/5120498

Also an extra tip.

Generally we use XPath just whena element is very hard, or even impossible to find with a ID or a CssSelector. It is because XPath is not common as CssSelector and it harder to understand depending how it is written.

When we need to find some element using XPath or CssSelector, first of all we need to check what values will not change after a interaction with the page, and also what provide us a unique value (when we need only one element, ofc).

Since your html has only one element with id txtUserPhoneNumber, and your desired element is inside it, let's start selecting it. After, look the tag name of the wanted element. There is only one input element inside it? Nice! So we can reach the element using only these few infos.

CssSelector:

#txtUserPhoneNumber input

And

Xpath:

//*[@id='txtUserPhoneNumber ']//input

Community
  • 1
  • 1
Striter Alfa
  • 1,577
  • 1
  • 14
  • 31
0

This error message...

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

...implies that the Locator Strategy you have adapted doesn't identifies any unique WebElement on the webpage.

However there are a couple of issues including the version compatibility between the binaries you are using as follows :

  • You are using chromedriver=2.35
  • Release Notes of chromedriver=2.35 clearly mentions the following :

Supports Chrome v62-64

  • You are using chrome=66.0
  • Release Notes of ChromeDriver v2.38 clearly mentions the following :

Supports Chrome v65-67

So there is a clear mismatch between the ChromeDriver version (v2.35) and the Chrome Browser version (v66.0)

Solution

  • Upgrade Selenium to current levels Version 3.11.0.
  • Upgrade ChromeDriver to current ChromeDriver v2.38 level.
  • Keep Chrome version at Chrome v66.x levels. (as per ChromeDriver v2.38 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • As you are seeing ElementNotVisibleException you need to induce WebDriverWait for the WebElement to be clickable as follows :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    # other code
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[@data-abide='ajax']//input[@ng-model='UserPhoneNew']"))).send_keys("08100000000")
    
  • Execute your @Test.

Note : Here you will find a detailed discussion on ElementNotVisibleException : Selenium Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352