简体   繁体   English

无法找到元素Selenium

[英]Unable to locate element Selenium

I'm trying to find a button to click using Selenium. 我正在尝试找到一个使用Selenium单击的按钮。 The portion of html containing the button is the following: 包含按钮的html部分如下:

<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() 然后我应该执行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. 您在css_selector的开头缺少一个点,因此它查找的元素是btn-standard而不是类。 And there is no such element as btn-standard 而且没有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 '.' 对于CSS选择器'。'。 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')]"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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