3

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.

Jorjani
  • 827
  • 1
  • 16
  • 31
  • 1
    Possible duplicate of [Is it possible to locate element by partial id match in Selenium](https://stackoverflow.com/questions/31248804/is-it-possible-to-locate-element-by-partial-id-match-in-selenium) – BugHunter May 11 '18 at 19:11
  • I'm following http://selenium-python.readthedocs.io/locating-elements.html for locating elements with Python and not sure if I can use the suggested solution there? – Jorjani May 11 '18 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 `` is added dynamically or its ID is changed. – Titus May 11 '18 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 is added dynamically. How does it change thigs? – Jorjani May 11 '18 at 19:23
  • The css selector was find, I'm not sure what library you're using but it needs to have some kind of function for waiting until an element is present on the page. Something like `driver.wait(until.find_element_by_css_selector('[id*=new]'), 1000)` – Titus May 11 '18 at 19:34
  • I'm using Python and tried time.sleep(5) to run the method after 5 seconds and I know the element is present then but still have the same issue. – Jorjani May 11 '18 at 19:35
  • @Jorjani , Check whether input node located inside an iframe – Andersson May 11 '18 at 19:44
  • @Andersson Hah! It is inside an iframe! – Jorjani May 11 '18 at 20:06
  • 1
    @Jorjani , then you need to [switch to iframe](https://computerrecipes.wordpress.com/2012/08/23/selenium-webdriver-interact-with-an-element-inside-an-iframe/) to handle elements inside – Andersson May 11 '18 at 20:08

1 Answers1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352