4

I'm using ChromeDriver of Selenium with Python and I'm trying to find a button on my page that has the following HTML:

<input id="j_id0:SiteTemplate:j_id255:new" type="submit" name="j_id0:SiteTemplate:j_id255:new" value="New" class="kbutton-white">

The only thing I know being constant is the id and name ending with "new" and I'm trying to use the following code to identify and click that element:

test_runner.driver.find_element_by_css_selector('[id*=new]').click()

However, I get this error when I run the code:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id*=new]"}

What's my mistake here?

Update: This element was inside an iframe and I had to switch to the iframe before trying to find the element. Please see comments for the answer.

9
  • 1
    Possible duplicate of Is it possible to locate element by partial id match in Selenium Commented May 11, 2018 at 19:11
  • I'm following selenium-python.readthedocs.io/locating-elements.html for locating elements with Python and not sure if I can use the suggested solution there? Commented May 11, 2018 at 19:16
  • Make sure the element is present on the page when you run that statement and its ID is the value you've mentioned. Is possible that the <input> is added dynamically or its ID is changed. Commented May 11, 2018 at 19:17
  • 1- I tried with ("[id$=new]") as suggested in the accepted answer on the other question and got the same error. 2- @Titus I can confirm that the id and name are there as I mentioned above. However, I know that <input> is added dynamically. How does it change thigs? Commented May 11, 2018 at 19:23
  • 1
    @Jorjani , then you need to switch to iframe to handle elements inside Commented May 11, 2018 at 20:08

1 Answer 1

2

As per the HTML you have shared to invoke click() on the desired element you can use the following css_selector :

driver.find_element_by_css_selector("input.kbutton-white[id$='new'][name$='new'][value='New']").click()

Explaination :

  • .kbutton-white : The class attribute.
  • id$='new' : id attribute ends with new
  • name$='new' : name attribute ends with new
  • value='New' : The value attribute.

But it seems the element is dynamic so you may need to induce WebDriverWait as follows :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.kbutton-white[id$='new'][name$='new'][value='New']"))).click()

References

You can find a couple of relevant detailed discussions in:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.