1
votes

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")
3
can you please provide the html of your page - Pritam Maske
have you tried find_element_by_name("PURCHASE_ORDER_CODE_NAME") - NarendraR
if possible can provide the link of your web page? - Pritam Maske
Welcome to SO. Use the Expected condition and wait for the element to be visible before performing the operation. I don't see any issue with the locator or the code. - supputuri
Is this element in an IFRAME? Have you tried a wait? - JeffC

3 Answers

1
votes

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

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
    
0
votes

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

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