首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元素位置中的Xpath问题

元素位置中的Xpath问题
EN

Stack Overflow用户
提问于 2019-03-11 06:27:58
回答 4查看 160关注 0票数 0

对于我选择的元素,我将获得一个很长的xpath。有时间缩短吗?这是我正在获得的xpath:

代码语言:javascript
复制
//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 --这是我必须清除弹出窗口的代码,正如我在下面的注释中提到的。

代码语言:javascript
复制
//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)
            {}
EN

回答 4

Stack Overflow用户

发布于 2019-03-11 06:37:40

您要查找的元素可以使用xpath找到:

代码语言:javascript
复制
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));

但是,由于在打开链接时不能直接看到元素,因此您将得到NoSuchElementException,因此要解决这个问题,可以对直接在页面的div上操作的元素使用javascript单击方法。

此外,我还可以看到,当我第一次打开页面时,会出现一个订阅弹出,因此您需要首先取消该弹出窗口(如果弹出窗口存在),然后使用JavaScript单击方法单击"Socks“元素。

您的代码应该如下所示:

代码语言:javascript
复制
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);
票数 1
EN

Stack Overflow用户

发布于 2019-03-14 12:07:11

要在“男子>>配件>>‘袜子”部分徘徊,您需要使用selenium Actions类。

因为第一次点击男人是不可能的(因为它会打开其他部分),所以要悬停到袜子上,你需要一次完成所有你想要实现的动作。

过程应该是:

  1. 先搬到男人身上
  2. 移到配饰
  3. 然后移动到短袜,点击它。

注释:通过使用Action,我们可以在一次运行中链接所有进程。如下所述,

1)第一条路:

代码语言:javascript
复制
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)等待的第二种方式:

代码语言:javascript
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2019-03-11 06:41:19

试试这个:

代码语言:javascript
复制
//a[normalize-space(text()) = 'Socks']

我建议您不要使用这么长的xpath,而是尝试自己编写xpath。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55096278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档