【发布时间】:2019-08-01 10:30:26
【问题描述】:
我选择的元素的 xpath 很长。有没有办法缩短它?这是我得到的 xpath:
//li[@class='menu_men 1-level hasChild']//div[contains(@class,'level-2')]//div[@class='menu-wrapper']//ul[@class='level-2']//li[@class='1-level']//div[@class='level-3']//ul[@class='level-3']//li//a[@class='level-3'][contains(text(),'Socks')]
这是网址:Calvin Klein Singapore 我将鼠标悬停在“男士”上,将出现配饰部分,然后将鼠标悬停在“袜子”上以获取 xPath。
我在我的代码中得到以下执行,我想知道长 xpath 是否可能是原因之一:
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法 定位元素:{"method":"xpath","selector":"//li[@class='first menu_men 1 级 hasChild']//div[contains(@class,'level-2')]//div[@class='menu-wrapper']//ul[@class='level-2']//li[@ class='1-level']//div[@class='level-3']//ul[@class='level-3']//li//a[@class='level-3'] [包含(文本(),'袜子')]“}
我正在使用 chrome 开发人员工具中的cropath 来获取 xPath。
我是自动化的新手,我真的希望有人能给我建议。谢谢。
@SameerArora 这是我必须清除弹出窗口的代码,正如我在下面的 cmets 中提到的那样。
//for clearing the popup window
@FindBy(how=How.XPATH,using="//*[starts-with(@id,'popup-subcription-closes-link-')]")
public WebElement newsletterpopup;
public String clickCategory(){
//.....
resusableFunctions.buttonClick(driver, newsletterpopup, "popoup");
}
public void buttonClick(WebDriver driver, WebElement element, String elementName) throws InterruptedException
{
try
{
element.click();
System.out.println("Log: ResuableFunction.buttonClick");
}
catch (org.openqa.selenium.ElementNotInteractableException notInteract)
{}
【问题讨论】: