对于我选择的元素,我将获得一个很长的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新加坡我徘徊在‘男士’,配件部分将出现,比我悬停‘袜子’以获得xPath。
我在我的代码中得到了以下内容,我想知道,不知怎么的,长xpath可能是原因之一:
'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'"}:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“//li@class=‘first menu_men 1-level hasChild'//divcontains(@class,org.openqa.selenium.NoSuchElementException)
我正在使用工具中的cropath来获取xPath。
我是自动化新手,我真的希望有人能给我建议。谢谢。
@SameerArora --这是我必须清除弹出窗口的代码,正如我在下面的注释中提到的。
//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)
{}发布于 2019-03-11 06:37:40
您要查找的元素可以使用xpath找到:
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));但是,由于在打开链接时不能直接看到元素,因此您将得到NoSuchElementException,因此要解决这个问题,可以对直接在页面的div上操作的元素使用javascript单击方法。
此外,我还可以看到,当我第一次打开页面时,会出现一个订阅弹出,因此您需要首先取消该弹出窗口(如果弹出窗口存在),然后使用JavaScript单击方法单击"Socks“元素。
您的代码应该如下所示:
List<WebElement> closeSubscriptionPopUp = driver.findElements(By.xpath("//a[contains(@id,'popup-subcription-closes-link')]"));
if (closeSubscriptionPopUp.size() > 0) {
closeSubscriptionPopUp.get(0).click();
}
WebElement sockElement = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", sockElement);发布于 2019-03-14 12:07:11
要在“男子>>配件>>‘袜子”部分徘徊,您需要使用selenium Actions类。
因为第一次点击男人是不可能的(因为它会打开其他部分),所以要悬停到袜子上,你需要一次完成所有你想要实现的动作。
过程应该是:
注释:通过使用Action,我们可以在一次运行中链接所有进程。如下所述,
1)第一条路:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]")))
.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")))
.click().build().perform();2)等待的第二种方式:
WebDriverWait wait= new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]"))).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.click().build().perform();发布于 2019-03-11 06:41:19
试试这个:
//a[normalize-space(text()) = 'Socks']我建议您不要使用这么长的xpath,而是尝试自己编写xpath。
https://stackoverflow.com/questions/55096278
复制相似问题