我无法使用硒单击下拉列表元素

时间:2019-10-15 16:23:33

标签: selenium automation dropdown

我想单击“ Mon profil” 我试过了:

driver.findElement(By.className("profile-navbar-label dropdown-item")).click()

driver.findElement(By.xpath("//button[contains(.,'" + "Mon profil" + "')]")).click()

它不起作用

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试这个Xpath

driver.findElement(By.xpath("//button[contains(.,'Mon profil')]")).click() 

OR

driver.findElement(By.xpath("//button[text()='Mon profil']")).click() 

答案 1 :(得分:0)

您不能使用带有空格的类名来查找元素。如果要使用这样的类名,则需要为此编写xpath。因此第一种查找方法是错误的,关于xpath,我不确定这是否是写下xpath的正确方法。

尝试使用此xpath = (.//button[@type='button'])[1]即可。

通过单击F12,然后单击xpath,可以检查ctrl+F是否在找到正确的Web元素。输入xpath,它将突出显示网络元素。

答案 2 :(得分:0)

首先,您应该单击选择框,当显示下拉列表时,使用以下代码

driver.findElement(By.xpath("//button[text()='Mon profil']")).click() 

OR

WebElement = driver.findElement(By.xpath("//button[text()='Mon profil']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", webElement);