wmoura12
wmoura12

Reputation: 11

Unable to locate element error while trying to locate an input box element using Selenium through Python

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")

Upvotes: 1

Views: 1431

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

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
    

Upvotes: 1

R. Zuini
R. Zuini

Reputation: 11

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

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

Upvotes: 0

Sameer Arora
Sameer Arora

Reputation: 4507

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()

Upvotes: 1

Related Questions