问题描述
有一个简单的带有文本的 div 标签。 如何从下面的标签构建 xpath。
WebElement element = driver.findElement(By.xpath("//div[@id='el2']"));
<DIV align=”left” id=e1>Insured</DIV>
Actions action = new Actions(driver);
//To mouseover on main menu
action.moveToElement(element).build().perform();
由于 id 每次都在变化,我需要使用 div 标签中的文本
1楼
如果您的 ID 不断变化,请在 Xpath 下尝试此操作。 这应该可以正常工作。
WebElement element = driver.findElement(By.xpath("//DIV[contains(text(), 'Contacts')]"))
或者
WebElement element = driver.findElement(By.xpath("//DIV[text()[contains(.,'Contacts')]]"))