![](/img/trans.png)
[英]Unable to click the 'button' in selenium webdriver (using Java)
[英]Unable to select a button using Selenium WebDriver in Java
尝试使用Selenium WebDriver从网页的可折叠区域中选择一个按钮。 我是WebDriver的新手,在解决如何获得这个特殊的小怪物方面遇到问题。
<a href="#" class="btn new-fields-button" data-fields="<div class='contributor-form form-inline form-nested'>
<select id="video_item_contributors_attributes_39041920_role" name="video_item[contributors_attributes]
[39041920][role]"><option value="Actor">Actor</option> <option
value="Director">Director</option> <option value="Writer">Writer</option>
<option value="Producer">Producer</option></select> <input class="input-small"
id="video_item_contributors_attributes_39041920_name" name="video_item[contributors_attributes][39041920][name]"
placeholder="Name" size="30" type="text" /> <a class='btn btn-danger delete-nested'
data-destroy-id='destroy-contributor-toggle-'> <i class='ss-trash'></i> </a> </div> "
data-id="39041920"><i class="ss-plus"></i> Add Contributor</a>
这是一段代码(试图将其分解为便于阅读的代码),描述了需要单击的按钮,这些按钮直接从Chrome的“检查元素”视图中复制而来。 有任何想法吗?
根据给出的信息,您可以像这样进行xpath搜索:
WebElement button = webDriver.findElement(By.xpath("//a[@data-id='39041920']"));
不知道data-id属性是否唯一,因为您对问题页面的了解并不多。
您需要首先确保a
元素不在iframe中。 如果是这样,则必须使用driver.switchTo().frame("frameName")
将上下文切换到框架。
然后找到与a
元素唯一且一致的东西,以便能够识别它。 下面是一些您可以使用的xpath定位器示例。
作为参考,请阅读XPath W3C Recommendation-它提供了使用xpath所需的所有信息。
通过锚文本
By.xpath("//a[text()=' Add Contributor']")
在xpath中使用class
值
By.xpath("//a[@class='btn new-fields-button']")
要进行部分匹配,可以使用contains()
By.xpath("//a[contains(@class,'new-fields-button')]")
您还可以使用多个选择器,例如:
By.xpath("//a[contains(@class,'new-fields-button') and contains(text(),'Add Contributor')]")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.