尝试使用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的“检查元素”视图中复制。有什么想法吗?
发布于 2013-12-30 19:24:25
首先,您需要确保a
元素不在iframe中。如果是,则必须使用driver.switchTo().frame("frameName")
将上下文切换到框架。
然后,找到与a
元素一致的唯一元素,以便能够识别它。下面是一些可以使用的xpath定位器示例。
要获得参考,请阅读XPath W3C推荐 -它提供了使用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')]")
发布于 2013-12-30 15:54:16
根据所提供的信息,您可以执行如下xpath搜索:
WebElement button = webDriver.findElement(By.xpath("//a[@data-id='39041920']"));
不知道,如果数据id属性是唯一的还是不唯一的,因为您并没有真正告诉您的页面的问题。
https://stackoverflow.com/questions/20849418
复制相似问题