1

I want to locate an element in selenium using css selector, and I use the program "copy css selector" and got:

div>button[type ="submit"]

Is this correct?

submit_button = driver.find_element_by_css_selector("input[type='submit']")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
PChao
  • 417
  • 2
  • 5
  • 17
  • 1
    You might want to provide a bit more information. What language and libraries are you using? Python? JS? Java? what versions? I assume the "copy css selector" is in Chrome or some similar browser. I'm pretty sure stackoverflow quality police are going to mark your question as lacking research, so do a bit of search in stackoverflow and you should find the answer. e.g. https://stackoverflow.com/questions/18288333/need-to-find-element-in-selenium-by-css You may find if there is more than one button on your page you may get more than one match for "submit" though. – Roochiedoor Jan 10 '21 at 19:42

1 Answers1

3

Yes, the Locator Strategy below:

submit_button = driver.find_element_by_css_selector("input[type='submit']")

is syntactically correct. But as per the copy css selector it should have been:

submit_button = driver.find_element_by_css_selector("div > button[type='submit']")

Note: find_element_by_* commands are deprecated. Please use find_element() instead

Accordingly you can also use:

submit_button = driver.find_element(By.CSS_SELECTOR, "input[type='submit']")

As per copy css selector:

submit_button = driver.find_element(By.CSS_SELECTOR, "div > button[type='submit']")
JeffC
  • 22,180
  • 5
  • 32
  • 55
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352