【问题标题】:How to click a href link using Selenium如何使用 Selenium 单击 href 链接
【发布时间】:2015-08-18 13:21:11
【问题描述】:

我有一个 html href 链接

<a href="/docs/configuration">App Configuration</a>

使用 Selenium 我需要点击链接。目前,我正在使用以下代码 -

Driver.findElement(By.xpath("//a[text()='App Configuration']")).click(); 

但它不会重定向到页面。我也试过下面的代码-

Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();

但这会抛出异常 -

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 13 milliseconds

链接可见且页面已完全加载。我不知道我的代码有什么问题。

【问题讨论】:

    标签: java selenium xpath css-selectors webdriverwait


    【解决方案1】:

    使用

    driver.findElement(By.linkText("App Configuration")).click()
    

    其他方法将是

    JavascriptLibrary jsLib = new JavascriptLibrary(); 
    jsLib.callEmbeddedSelenium(selenium, "triggerMouseEventAt", elementToClick,"click", "0,0");
    

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);
    

    详细解答,View this post

    【讨论】:

      【解决方案2】:

      好像a 标签被隐藏了。请记住 Selenium 无法与隐藏元素交互。在这种情况下,Javascript 是唯一的选择。

      By css = By.cssSelector("a[href='/docs/configuration']");
      WebElement element = driver.findElement(css);
      ((JavascriptExecutor)driver).executeScript("arguments[0].click();" , element);
      

      【讨论】:

      • 得到 cucumber.runtime.CucumberException: org.openqa.selenium.WebDriverException: arguments[0] is undefined
      • 尝试((JavascriptExecutor)driver).executeScript("document.querySelector(\"a[href='/docs/configuration']\").click();");而不是答案中的3行
      • org.openqa.selenium.WebDriverException: document.querySelector(...) is null
      • 对我来说似乎是等待问题。添加一些明确的等待并尝试
      【解决方案3】:
       webDriver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();
      

      以上行工作正常。请去掉href后面的空格。

      该元素在页面中是否可见,如果该元素不可见,请向下滚动页面然后执行点击操作。

      【讨论】:

        【解决方案4】:

        你可以使用这个方法:

        对于链接,如果您使用linkText();,它比任何其他定位器都更有效。

        driver.findElement(By.linkText("App Configuration")).click();
        

        【讨论】:

        • 你忘记了括号。
        • 不适用于基于用户 IP 语言的页面
        【解决方案5】:

        你可以使用xpath如下,试试这个:

        driver.findElement(By.xpath("(.//[@href='/docs/configuration'])")).click();
        

        【讨论】:

          【解决方案6】:

          对这样的元素使用明确的wait

          WebDriverWait wait1 = new WebDriverWait(driver, 500);
          wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("path of element"))).click();
          

          【讨论】:

            【解决方案7】:

            如何在不使用 selenum 中的 click 方法的情况下点击链接?

            这是一个棘手的问题。请按照以下步骤操作:

            driver.get("https://www.google.com");
            String gmaillink= driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm&ogbl']")).getAttribute("href");
            System.out.println(gmaillink);
            driver.get(gmaillink);
            

            【讨论】:

              【解决方案8】:

              尝试使用 Action 类到达元素

              Actions action = new Actions(driver);
              action.MoveToElement(driver.findElement(By.xpath("//a[text()='AppConfiguration']")));
              action.Perform();
              

              【讨论】:

                【解决方案9】:

                这是你的代码:

                Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();
                

                你错过了引号

                应该是这样的

                Driver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();
                

                希望这会有所帮助!

                【讨论】:

                  【解决方案10】:

                  到元素上的click(),文本为App Configuration,您可以使用以下Locator Strategies

                  • linkText:

                    driver.findElement(By.linkText("App Configuration")).click();
                    
                  • cssSelector:

                    driver.findElement(By.cssSelector("a[href='/docs/configuration']")).click();
                    
                  • xpath:

                    driver.findElement(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']")).click();
                    

                  理想情况下,在需要为elementToBeClickable() 诱导WebDriverWait 的元素上click(),您可以使用以下任一Locator Strategies

                  • linkText:

                    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("App Configuration"))).click();
                    
                  • cssSelector:

                    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/docs/configuration']"))).click();
                    
                  • xpath:

                    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']"))).click();
                    

                  参考文献

                  您可以在以下位置找到一些相关的详细讨论:

                  【讨论】:

                    【解决方案11】:

                    这些答案似乎有点太复杂了。这对我有用。 给 svg 标签一个 id,然后通过该 id 找到元素:

                    这是你的 svg:

                    <svg id="element-id"><use href="/...."></use></svg>
                    

                    然后这样做:

                    driver.findElement(By.id("element-id")).click();
                    

                    【讨论】:

                      猜你喜欢
                      • 2021-12-03
                      • 2018-02-08
                      • 1970-01-01
                      • 2016-11-06
                      • 2020-07-14
                      • 1970-01-01
                      • 1970-01-01
                      • 2022-08-11
                      • 1970-01-01
                      相关资源
                      最近更新 更多