Selenium Can';t使用xpath单击下拉列表

Selenium Can';t使用xpath单击下拉列表,selenium,xpath,selenium-webdriver,Selenium,Xpath,Selenium Webdriver,我试着点击链接“ББаа”,但没有成功。我做错了什么 driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() -works driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() - doesn't work. Why? 很可能带有WebEleme

我试着点击链接“ББаа”,但没有成功。我做错了什么

driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText()  -works
driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() - doesn't work. Why?

很可能带有
WebElement.getText()
的行可以工作,但是带有
WebElement.click()的行不能工作,如下所示:

driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() //works 
driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).click() //doesn't work. 
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='filter-title' and text()='Бренд']"))).click();
解释 此行为的原因是,当客户端(即Web浏览器)在'document.readyState'等于“complete”时将控件返回到WebDriver实例时,很可能预期的WebElement是存在的(元素存在,但不一定意味着元素是可交互的)和可见的,即(元素是可见的,并且其高度和宽度大于0)。因此,您能够提取文本ББаа

但是网页元素是存在的可见的,但并不意味着网页元素也是可点击的,即可交互的

在这里,您可以找到有关的详细讨论

解决方案 若要单击文本为的WebElement,则必须按如下方式引导WebDriverWait:

driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).getText() //works 
driver.findElement(By.xpath("//div[@class='filter-title' and text()='Бренд']")).click() //doesn't work. 
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='filter-title' and text()='Бренд']"))).click();

如果我能够从您的图像中正确提取信息,则该类已经是唯一标识符。您只需使用
driver.findElement(By.className(“brand”)。单击();

如果第二类“brand”没有在其他任何地方使用,那么这应该是有效的。如果这不起作用,那么只需使用包含它的div的类:

driver.findElement(By.className("initialDivClass")).findElement(By.className("brand")).click();

您有相同的代码行,如何可能一行工作而另一行不工作?要能够单击一个元素,它应该是可见的,而不是被另一个元素“覆盖”。您可能需要在执行单击操作之前添加延迟,以确保元素完全可见