英文:
How to choose an element from divs having the same classnames (Selenium)
问题
我对Selenium还很陌生,所以我卡住了。
这里有一张截图显示检查元素,两个div几乎是相同的,但它们内部都有另一个包含不同文本值的div:图片。
我能否通过检查getText()
是否等于...来选择我想要的div呢?我找到了以下方法,但据我所知,我永远不会知道流的顺序:
new ArrayList<>(
chromeDriver.findElements(By.xpath("//div[@class='sign-up-row']"))
).get(1).click();
谢谢大家。
英文:
I am new to Selenium which is probably why I am stuck.
Here is the screenshot showing inspect where two divs are almost identical but they both have another div inside containing different text values: Image.
Could I somehow choose the div I want by checking if getText()
is equal to ...?
I found the following way but as far as I know, I would never know the order of the stream:
new ArrayList<>(chromeDriver.findElements(By.xpath("//div[@class='sign-up-row']"))).get(1).click();
Thank you guys.
答案1
得分: 0
你可以使用在 div 标签内的标签中出现的文本来创建一个 xpath。这将返回你所感兴趣的特定元素。以下是 xpath 的示例:
//div/div[text()='textforcomparison']
英文:
You can create an xpath using the text present in the tag within the div tag. This would return the specific element you are interested in.
Example for the xpath:
//div/div[text()='textforcomparison']
答案2
得分: 0
你可以按照以下方法进行操作。
List<WebElement> lst = driver.findElements(By.xpath("//div[@class='sign-up-row']"));
for (int i = 0; i < lst.size(); i++) {
if(lst.get(i).getText().equals("要比较的文本")) {
lst.get(i).click();
//如果在匹配后要终止循环
break;
}
}
英文:
You can follow below approach.
List<WebElement> lst = driver.findElements(By.xpath("//div[@class='sign-up-row']"));
for (int i = 0; i < lst.size(); i++) {
if(lst.get(i).getText().equals("Text to Be Compared")) {
lst.get(i).click();
//If you want to break the foe loop after the match
break;
}
}
答案3
得分: 0
使用Xpath如下所示:
示例:对于//div[@class='sign-up-row']
这个节点
使用 text()
:
//div[@class='sign-up-row' and .//span[text()='Remember me.')]]
或者使用 contains
:
//span[contains(text(), 'Remember')]/ancestor::div[@class='sign-up-row']
使用点 .
可以找到内部元素:
//div[@class='sign-up-row' and contains(., 'Remember')]
英文:
Using Xpath like below:
example for //div[@class='sign-up-row']
this node
use text()
:
//div[@class='sign-up-row' and .//span[text()='Remember me.')]]
Or use with contains
:
//span[contains(text(), 'Remember')]/ancestor::div[@class='sign-up-row']
Use the dot .
can find inner element:
//div[@class='sign-up-row' and contains(., 'Remember')]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论