【问题标题】:Xpath issue in element location元素位置中的 Xpath 问题
【发布时间】: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)
            {}

【问题讨论】:

    标签: java selenium xpath


    【解决方案1】:

    试试这个:

    //a[normalize-space(text()) = 'Socks']
    

    我建议你不要使用这么长的 xpath 并尝试自己编写 xpath。

    【讨论】:

      【解决方案2】:

      要将鼠标悬停在“男士”>>配饰>>“袜子”部分,您需要使用 selenium Actions 类。

      因为实际上不可能首先点击男性(因为它会打开其他部分), 因此,要将鼠标悬停在袜子上,您需要一次性链接您想要实现的所有操作。

      流程应该是:

      1. 首先移动到男性元素
      2. 转到附件
      3. 然后移至 Socks 并单击它。

      注意:通过使用 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();
      

      【讨论】:

      • 大家好,感谢这里的 cmets。我不再从事这个项目,但我发现自动化和硒很有趣,并会尝试找时间了解更多关于这些技术的信息。再次感谢。
      【解决方案3】:

      试试:

      //li[contains(@class,'menu_men')]//a[contains(text(),'Socks')]
      

      【讨论】:

      • 请为您的回答添加一些解释:您改变了什么以及为什么?
      【解决方案4】:

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

      WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
      

      但是,当您打开链接时,该元素不直接可见,您将得到 NoSuchElementException,因此您可以在直接操作页面 div 的元素上使用 javascript click 方法来解决它。
      除此之外,当我第一次打开页面时,我可以看到订阅弹出窗口出现,因此您需要先关闭该弹出窗口(如果存在弹出窗口),然后使用 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);
      

      【讨论】:

      • 你测试你的代码了吗? MEN 部分似乎是一个伪元素。
      • @DebanjanB 我确实检查了元素,我已经在这里上传了带有 xpath 的屏幕截图:imgur.com/6cABT5Q 请检查并让我知道我是否在某处犯了任何错误。
      • 您正在通过 Dev Tools 协议验证 xpath,因此得到解决。 IMO,Selenium 将无法直接通过 findElement() 解决它,因为它是一个 pseudo 元素。这就是为什么我问您是否以编程方式进行测试?结帐一次,让我们知道。
      • 我确实尝试运行代码,动作类移动到元素方法工作正常,但 click() 方法没有被执行。所以,我现在正在更新我的答案。谢谢!!
      • move to element() 用于哪个元素?我相信 Socks 具有 style 作为 hidden
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多