[英]Unable to select a button using Selenium WebDriver in Java
Trying to select a button from a collapsible area of a web page using Selenium WebDriver. 尝试使用Selenium WebDriver从网页的可折叠区域中选择一个按钮。 I'm very new to WebDriver, and am having issues figuring out how to get this particular little monster.
我是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>
That's the section of code (tried to break it up to be readable here) that describes the button that neds to be clicked, copied directly from Chrome's "Inspect Element" view. 这是一段代码(试图将其分解为便于阅读的代码),描述了需要单击的按钮,这些按钮直接从Chrome的“检查元素”视图中复制而来。 Any ideas?
有任何想法吗?
Based on the info given, you could do an xpath search like this: 根据给出的信息,您可以像这样进行xpath搜索:
WebElement button = webDriver.findElement(By.xpath("//a[@data-id='39041920']"));
No idea, if the data-id attribute is unique or not though, as you don't really tell much about your page in the question. 不知道data-id属性是否唯一,因为您对问题页面的了解并不多。
You need to ensure first that the a
element is not within an iframe. 您需要首先确保
a
元素不在iframe中。 If it is, you will have to switch context to the frame using driver.switchTo().frame("frameName")
. 如果是这样,则必须使用
driver.switchTo().frame("frameName")
将上下文切换到框架。
Then find something that is unique and consistent with the a
element to be able to identify it. 然后找到与
a
元素唯一且一致的东西,以便能够识别它。 Some example xpath locators you can use are below. 下面是一些您可以使用的xpath定位器示例。
For reference, read XPath W3C Recommendation - it provides all the info you will ever need for working with xpaths. 作为参考,请阅读XPath W3C Recommendation-它提供了使用xpath所需的所有信息。
By anchor text 通过锚文本
By.xpath("//a[text()=' Add Contributor']")
Using the class
value with xpath 在xpath中使用
class
值
By.xpath("//a[@class='btn new-fields-button']")
To do a partial match, you can use contains()
要进行部分匹配,可以使用
contains()
By.xpath("//a[contains(@class,'new-fields-button')]")
You can also use multiple selectors, such as: 您还可以使用多个选择器,例如:
By.xpath("//a[contains(@class,'new-fields-button') and contains(text(),'Add Contributor')]")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.