简体   繁体   English

访问 class 名称 Selenium Webdriver

[英]accessing the class name Selenium Webdriver

I am trying to access a web element in this example I am trying to access the className我正在尝试访问此示例中的 web 元素 我正在尝试访问 className

driver.findElement(By.className("more-option")).click(); driver.findElement(By.className("more-option")).click();

from bellow but it fails.从下面,但它失败了。

<div class="text-center">
  <a class="small text-muted js-more-options" href="#">More
  Options</a> = $0
</div>

My goal is to be able to test the ability to click More options button我的目标是能够测试点击更多选项按钮的能力

Edit编辑

I have tried我努力了

driver.findElement(By.cssSelector("td[title='More options']")).click();

and

driver.findElement(By.partialLinkText("options")).click();

Find element using By.className just for single class name.仅使用By.className查找单个 class 名称的元素。

Try following approach.尝试以下方法。

By css selector:通过 css 选择器:

driver.find_element_by_css_selector('div.text-center a.small.text-muted.js-more-options').click()

driver.find_element_by_css_selector('a[class="small text-muted js-more-options"]').click()

By xpath:通过 xpath:

driver.find_element_by_xpath('//div[@class="text-center"]//a[@class="small text-muted js-more-options"]').click()

By partial link text:通过部分链接文本:

driver.find_element_by_partial_link_text('Options').click()

There are to many option you can click on element.You can use contains().您可以单击元素有很多选项。您可以使用 contains()。 Contains() is a method used in XPath expression. Contains() 是 XPath 表达式中使用的方法。

driver.findElement(By.XPath("//a[contains(text(),'More Options')]")).Click();

or或者

driver.findElement(By.XPath("//a[contains(@class,'small')]")).Click();

if you get more than one element then you have to use index and click on particular element.如果您获得多个元素,则必须使用索引并单击特定元素。

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

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