简体   繁体   中英

Unable to locate element Selenium

I'm trying to find a button to click using Selenium. The portion of html containing the button is the following:

<button class="btn-standard call-to-action">Login</button>

I am trying to find it with:

btn = driver.find_element_by_css_selector("btn-standard.call-to-action")

And then i should execute btn.click()

But when i try to run the code i get this error:

 no such element: Unable to locate element: {"method":"css 
selector","selector":"btn-standard.call-to-action"}

How can I fix this?

Try this

btn = driver.find_element_by_css_selector(".btn-standard.call-to-action")

You are missing a dot at the beginning of css_selector so it looks for an element btn-standard and not a class. And there is no such element as btn-standard

Also you can try element type with class like so:

btn = driver.find_element_by_css_selector("button.btn-standard")

Or any mix of class and element type

For css selectors '.' is appended in the beginning only for class. Can you try this:

       btn = driver.find_element_by_css_selector("button.btn-standard")

您甚至可以使用xpath:

btn = driver.findElement(By.xpath("//button[contains(text(),'Login')]"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM