1

I installed chropath to finding out the xpath for websites.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\\Users\\ADMIN\\Downloads\\chromedriver_win32\\chromedriver.exe")

driver.get("https://kite.zerodha.com")

username = driver.find_element_by_xpath("//input[@placeholder='User ID']")
username.send_keys("abcc")

I wanted to find username xpath using chropath and it gave me //input[@placeholder='User ID'] but it is still giving me NoSuchElementException error. I thought chropath extension would always give me correct xpath. What can be the reason for this ?

This is the code that I get when I inspect Username

<input type="text" placeholder="User ID" autocorrect="off" maxlength="6" autofocus="autofocus" autocapitalize="characters" animate="true" label="" rules="[object Object]" dynamicwidthsize="8" xpath="1">
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

2

Functionally, was correct to find the for the desired element. However as the element is having the attribute animate="true", when the element recieves the cursor focus the attribute placeholder="User ID" gets changed as a result Selenium is unable to locate the element.


Solution

To send a character sequence within the User ID field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH:

    driver.get('https://kite.zerodha.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='User ID']//following-sibling::input[1]"))).send_keys("TANMAY")
    
  • Using CSS_SELECTOR:

    driver.get('https://kite.zerodha.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.su-input-label.su-dynamic-label + input"))).send_keys("TANMAY")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

zerodha

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you @DebanjanB. You can also generate xpath in ChroPath using static attribute. 1. Click on setting icon available in ChroPath tab 2. In attribute name box, type attribute value & hit enter 3. It will generate xpath with given attribute if that will be unique else will try with other attribute. 4. If given attribute is not there for element, it will generate xpath with other attr. 5. Also for few attr sort cuts have been provided. If you don't want to generate xpath with them, uncheck them. – SelectorsHub ChroPath Creator Feb 26 '20 at 07:01
  • 1
    @SanjayKumarIChroPathCreator Great news, though I haven't used [tag:chropath] personally but I am aware _Chropath_ have made life a lot easier for software professionals using XPath. Thanks for the innovative approach. – undetected Selenium Feb 26 '20 at 07:08
0

I checked your code and I think it is correct, however, I think you might be missing a .click() element. To make sure that the elements are present you could do the following, where I've added optional wait time for elements to be loaded.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Handle wait time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

driver = webdriver.Chrome("C:\\Users\\ADMIN\\Downloads\\chromedriver_win32\\chromedriver.exe")

driver.get("https://kite.zerodha.com")

wait = WebDriverWait(driver, 60)

username = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='User ID']")))
username.click()
username.send_keys("abcc")
Philip
  • 944
  • 11
  • 26