无法使用xpath单击下拉列表

时间:2018-03-05 10:49:37

标签: 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?

enter image description here

2 个答案:

答案 0 :(得分:1)

使用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. 

此行为的原因是当客户端(即 Web浏览器)将控件返回到 WebDriver 实例时'document.readyState'等于“完成”,目标 WebElement 很可能存在 < em>(元素存在,但并不一定意味着元素可以相互作用)和可见(元素是可见的,高度和宽度也大于0 )。所以你能够提取文字Бренд

WebElement 存在可见但不会认为 WebElement 可点击可互动

您可以在此处找到有关'document.readyState' equal to "complete"

的详细讨论

解决方案

要点击 WebElement ,文字为Бренд,您必须按如下方式诱导 WebDriverWait

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

答案 1 :(得分:0)

如果我能够从您的图像中正确提取信息,那么该类已经是唯一标识符。您可以使用driver.findElement(By.className("brand")).click();

如果二级品牌&#34;品牌&#34;不在其他任何地方使用。如果这不起作用,那么只需要包含它的div的类:

driver.findElement(By.className("initialDivClass")).findElement(By.className("brand")).click();
相关问题