1

I´m trying to find a text box with Python and Selenium.. I Tried by_css_selector, bY XPATH, by ID, by name but the message is always the same:

Unable to locate element: #x-auto-225-input

this is the piece of html. I Want to find the textbox to fill it.

<td class="x-table-layout-cell" role="presentation" style="padding: 2px;">
    <div role="presentation" class=" x-form-field-wrap  x-component" id="x-auto-225" style="width: 150px;"></div>
    <input type="text" class=" x-form-field x-form-text " id="x-auto-225-input" name="PURCHASE_ORDER_CODE_NAME" tabindex="0" style="width: 150px;">
</td>

My last attempt was:

pc = browser.find_element_by_css_selector("#x-auto-225-input").click()
pc.send_keys("7555425-1")
JeffC
  • 22,180
  • 5
  • 32
  • 55
wmoura12
  • 11
  • 2

3 Answers3

1

Looking at the html, id mentioned can be dynamic, so you can't put the static id in your identifier.
However, as name attribute is present in the html, you can use that to identify your element, like:

browser.find_element_by_name("PURCHASE_ORDER_CODE_NAME").click()

Updated answer as per discussion with the OP

As an iframe is present on the UI, you need to first switch to the iframe and then click on the element.
To switch to iframe you can use:

browser.switch_to.frame(browser.find_element_by_tag_name('iframe'))

and then use:

pc = browser.find_element_by_name("PURCHASE_ORDER_CODE_NAME")
pc.click()
pc.send_keys("7555425-1")

if you want to switch back to the default content, you can use:

browser.switch_to.default_content()
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • I´ve tried :Unable to locate element: [name="PURCHASE_ORDER_CODE_NAME"] – wmoura12 Mar 28 '19 at 13:50
  • @wmoura12 can you check that is there any iframe available in the html ? – Sameer Arora Mar 28 '19 at 13:51
  • @wmoura12 i have updated my answer, please check the updated answer. – Sameer Arora Mar 28 '19 at 14:00
  • @wmoura12 let me know if the updated answer works for you – Sameer Arora Mar 28 '19 at 14:23
  • Tks a lot. IT Works, but the send_keys failed.browser.switch_to.frame(browser.find_element_by_name("gwtIndex")) pc = browser.find_element_by_name("PURCHASE_ORDER_CODE_NAME").click() pc.send_keys("7555425-1") - 'NoneType' object has no attribute 'send_keys' – wmoura12 Mar 28 '19 at 14:33
  • @wmoura12 have updated my answer. Please check now and let me know if it works – Sameer Arora Mar 28 '19 at 14:38
  • 1
    Your first code block has `pc = ....click()`. `.click()` returns `void` so nothing is going to get assigned into `pc` and it will likely throw an error. You've got it right in the third block, just update the first block to remove the `pc =` to avoid the problem. – JeffC Mar 28 '19 at 15:00
  • @JeffC yeah actually i updated the answer after discussion with the OP, just missed to update the first block. Thanks for mentioning it !! – Sameer Arora Mar 28 '19 at 15:00
  • @wmoura12 Take a look at his third block of code again and sync your code up to match. I think you are using an outdated version of the code. See my comment up two for why you are getting the error. – JeffC Mar 28 '19 at 15:02
1

The desired element is a dynamic element so to invoke click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.x-form-field.x-form-text[id$='-input'][name='PURCHASE_ORDER_CODE_NAME']"))).click();
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class=' x-form-field x-form-text ' and contains(@id,'-input')][@name='PURCHASE_ORDER_CODE_NAME']"))).click();
    
  • 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
  • Tks for your response. This code returned Timeout. TimeoutException: Message: – wmoura12 Mar 28 '19 at 14:38
  • _TimeoutException_ is the outcome of **failed** _expected-conditions_. Debug your code through `find_element_by_*` in-conjunction with `time.sleep()`. If you are able to locate the element, update the question with the observations. – undetected Selenium Mar 28 '19 at 15:33
0

Maybe you can try another "selector" approach. Ex(Javascript):

selenium.By.xpath('//*[@data-icon="edit"]')
driver.findElement(by).click()
R. Zuini
  • 1
  • 2