注销按钮的CSS选择器 [英] Css Selector for the Log Out button
问题描述
我在页面中有一个注销"按钮.我正在尝试在Selenium中使其自动化.以下是该元素的源代码.
I have a LogOut button in a page. I am trying to automate it in Selenium.Following is the source code for that element.
<a class="_2k0gmP" data-reactid="53" href="#">Log Out</a>
这是我正在使用的代码
driver.findElement(By.cssSelector("._2k0gmP[text='Log Out']"));
但是我反复得到No Such Elemet found异常,有时是Invalid Selector Exception.有人可以帮我吗?
But i am repeatedly getting No Such Elemet found exception and sometimes Invalid Selector Exception. Could somebody please help me with this ?
推荐答案
您将获得InvalidSelectorException
,因为您无法使用cssSelector
来基于其内部HTML文本查找元素.为此,您可以使用xpath或linkText选择器.
You are getting InvalidSelectorException
because you can't use cssSelector
to find element based on it's inner HTML text. For that you could use xpath or linkText selectors.
xpath:driver.findElement(By.xpath(".//a[text()='Log Out']");
linkText:driver.findElement(By.linkText("Log Out"));
linkText: driver.findElement(By.linkText("Log Out"));
NoSuchElement
异常可能是由于您像@gecki所说的那样在类选择器中省略了_
而抛出的,所以您的选择器正在搜索不存在的类.
NoSuchElement
exception was probably thrown because you left out _
in class selector as @gecki said so your selector was searching for class which doesn't exist.
这篇关于注销按钮的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!