简体   繁体   English

无法使用Java中的Selenium WebDriver选择按钮

[英]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="&lt;div class='contributor-form form-inline form-nested'&gt; 
&lt;select id=&quot;video_item_contributors_attributes_39041920_role&quot; name=&quot;video_item[contributors_attributes]
[39041920][role]&quot;&gt;&lt;option value=&quot;Actor&quot;&gt;Actor&lt;/option&gt; &lt;option 
value=&quot;Director&quot;&gt;Director&lt;/option&gt; &lt;option value=&quot;Writer&quot;&gt;Writer&lt;/option&gt; 
&lt;option value=&quot;Producer&quot;&gt;Producer&lt;/option&gt;&lt;/select&gt; &lt;input class=&quot;input-small&quot; 
id=&quot;video_item_contributors_attributes_39041920_name&quot; name=&quot;video_item[contributors_attributes][39041920][name]&quot; 
placeholder=&quot;Name&quot; size=&quot;30&quot; type=&quot;text&quot; /&gt; &lt;a class='btn btn-danger delete-nested' 
data-destroy-id='destroy-contributor-toggle-'&gt; &lt;i class='ss-trash'&gt;&lt;/i&gt; &lt;/a&gt; &lt;/div&gt; " 
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.

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