【发布时间】:2021-06-17 14:15:29
【问题描述】:
两个按钮的按钮类名相同如何识别元素 enter image description here
button clase naem rae same only the div class name are different
【问题讨论】:
标签: selenium selenium-webdriver
两个按钮的按钮类名相同如何识别元素 enter image description here
button clase naem rae same only the div class name are different
【问题讨论】:
标签: selenium selenium-webdriver
如果该类确实是识别这些元素的最佳方法,我想您最好通过使用该类获取两个元素并以编程方式区分它们来做到这一点。我正在编写伪代码,因为您没有指定编码语言:
webelement array buttons = driver.findElementsByCss('button.ss-button.ss-button.ss-button--secondary')
firstButton = buttons[0]
secondButton = buttons[1]
【讨论】:
xpath
for first button: (//input[@class='your-class-name')[1]
for second button: (//input[@class='your-class-name')[2]
css 选择器
for first button: input[class='your-class-name']:nth-child(1)
for second button : input[class='your-class-name']:nth-child(2)
Java 代码:
WebElement button1 = _driver.findElements(By.cssSelector(".your-class-name")).get(0);
WebElement button2 = _driver.findElements(By.cssSelector(".your-class-name")).get(1);
【讨论】: