0

I am trying to login into a webpage using Python Selenium. However, I am not getting the correct response. The input box is under an HTML table in the web.

This is what I have tried after looking up some tutorials online.

driver.find_element_by_xpath("//table[5]/tbody/tr[1]/td[2]/input").send_keys("hi")

The HTML code:

<html>

<head>...</head>

<body>

  <form name="form" action method="post" onsubmit>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">...</table>

    <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">
      <tbody>
        <tr align="left">
          <td class="mandatory"> USER ID</td>
          <td class="normal">
            <input class="subject" type="text" name="username" size="35" maxlength="10" onkeypress="navigate();">
          </td>
        </tr>
      </tbody>
    </table>

I am interested to pick out the element at the input class = "subject", which is the fifth table.

The following is the xPath code copied from Chrome inspector:

/html/body/form/table[5]/tbody/tr[1]/td[2]/input
u32i64
  • 2,384
  • 3
  • 22
  • 36
Wen Jiaxin
  • 15
  • 1

2 Answers2

0

Here is the xpath that you can use.

 //form[@name='form']/table[5]//input[@name='username']

enter image description here

You can also use the other xpath shown in the screenshot.

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Hi supputuri, thanks for the prompt reply. However, I am hit by with this error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//form[@name ='form']/table[5]//input[@name= 'username']"} – Wen Jiaxin Jun 20 '19 at 02:28
  • Are you sure you have only the html shared in OP? I am able to successfully enter the data. Check if you have any extra iframes in your application if it's different from the html that you posted in OP. – supputuri Jun 20 '19 at 02:32
  • That's right, it was indeed embedded in an iframes. I only understood it after going through a few rounds of web scrapping for other webs. Thanks! – Wen Jiaxin Jul 23 '19 at 01:01
0

To send a character sequence to the login <input> element associated with the text USER ID you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.subject[name='username'][onkeypress^='navigate']"))).send_keys("Wen Jiaxin")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='subject' and @name='username'][starts-with(@onkeypress, 'navigate')]"))).send_keys("Wen Jiaxin")
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352