Selenium webdriver 如何从使用CSS定位器定位的Web元素中查找锚文本?
我在HTML页面中有一个带有id、href等属性的锚列表Selenium webdriver 如何从使用CSS定位器定位的Web元素中查找锚文本?,selenium-webdriver,css-selectors,Selenium Webdriver,Css Selectors,我在HTML页面中有一个带有id、href等属性的锚列表 <a id="vRecords534534_link" onclick="theEvent=event;openModal(this.href, {width: 640});return false;" href="https://example/sourceDetailAction.do?NoticeId=534615198&webFlow=gcaMain">SIXER</a> <a id="vR
<a id="vRecords534534_link" onclick="theEvent=event;openModal(this.href, {width: 640});return false;" href="https://example/sourceDetailAction.do?NoticeId=534615198&webFlow=gcaMain">SIXER</a>
<a id="vRecords64353_link" onclick="theEvent=event;openModal(this.href, {width: 640});return false;" href="https://example/sourceDetailAction.do?NoticeId=534615198&webFlow=gcaMain">CROSS</a>
等等。查看具有起始和结束文本的id属性
我使用CSS定位器来实现这一点:
@FindBy(how=How.CSS, using="a[id^='vRecords'][id$='link']")
protected List<WebElement> vRecordsList;
public void getVendorRecordList(){
System.out.println(vRecordsList.size());
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("value"));
}
}
@FindBy(how=how.CSS,使用=“a[id^='vRecords'][id$='link']”)
受保护名单;
public void getVendorRecordList(){
System.out.println(vRecordsList.size());
for(WebElement s:vRecordsList){
System.out.println(“记录列表::”+s.getAttribute(“值”);
}
}
从上面的代码中,我可以找到所有带有vRecords且以link结尾的ids星星
问题:由此,我想从上面的锚链接中获得每个链接的锚定值,如“SIXER”、“CROSS”
如果任何其他方法都是可行的,那么也要还原。
getAttribute
基本上用于获取元素属性。但是在这里,您希望得到a
中的文本。为此,selenium提供了一种方法getText()
,如下所示:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
已编辑:-如果不幸的是getText()
在此处不起作用,请尝试使用getAttribute(“innerHTML”)
,如下所示:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
或者使用getAttribute(“textContent”)
如下:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
希望有帮助。:)
getAttribute
基本上用于获取元素属性。但是在这里,您希望得到a
中的文本。为此,selenium提供了一种方法getText()
,如下所示:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
已编辑:-如果不幸的是getText()
在此处不起作用,请尝试使用getAttribute(“innerHTML”)
,如下所示:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
或者使用getAttribute(“textContent”)
如下:-
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getText());
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("innerHTML"));
}
for(WebElement s : vRecordsList){
System.out.println("Record List ::"+s.getAttribute("textContent"));
}
希望对您有所帮助:)请回复。这样做可行吗?或者需要某种其他方式/方法。您的问题是什么?使用
.getText()
应该可以。。。您遇到了什么错误?@Robot请返回并尝试更新答案,如果帮助接受答案并将此问题标记为已解决…:)请回答。这样做可行吗?或者需要某种其他方式/方法。您的问题是什么?使用.getText()
应该可以。。。您遇到了什么错误?@Robot请回来尝试更新答案,如果有人帮助接受答案并将此问题标记为已解决…:)@Robot尝试更新答案希望有帮助…:)@Robot尝试更新答案希望有帮助…)