Javascript 无法单击网页中的任何链接

Javascript 无法单击网页中的任何链接,javascript,selenium,Javascript,Selenium,我正在使用SeleniumWebDriver来自动化网页。我的selenium代码没有标识链接。我得到以下错误 线程“main”org.openqa.selenium.NoSuchElementException中的异常: 没有此类元素:无法定位元素: {“方法”:“xpath”,“选择器”:“/html/body/font/font/b/a[2]”(会话) 信息:chrome=44.0.2403.89) 这是我正在使用的代码 public static void main(String[] a

我正在使用SeleniumWebDriver来自动化网页。我的selenium代码没有标识链接。我得到以下错误

线程“main”org.openqa.selenium.NoSuchElementException中的异常: 没有此类元素:无法定位元素: {“方法”:“xpath”,“选择器”:“/html/body/font/font/b/a[2]”(会话) 信息:chrome=44.0.2403.89)

这是我正在使用的代码

public static void main(String[] args) 
{       
    System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe");

         WebDriver driver = new ChromeDriver(); 
         driver.get("url");
        driver.findElement(By.xpath("/html/body/font/font/b/a[2]")).click();
}

提前感谢

有两种可能的情况

1) 您可能键入了错误的url

2) 您期望的元素xpath错误

请使用

本条例草案:


有两种可能的情况

1) 您可能键入了错误的url

2) 您期望的元素xpath错误

请使用

本条例草案:


如果您得到的异常是
NoTouchElementException
,可能有以下原因:-

  • 可能您使用的是不正确的
    xpath
    ,因此您需要共享HTML以获得更好的定位器解决方案

  • 当您要查找元素时,它可能不会出现在
    DOM
    上,因此您应该实现
    WebDriverWait
    以等待元素可见并可单击,如下所示:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    
  • 此元素可能位于任何
    iframe
    内。如果是,您需要在查找元素之前切换该
    frame
    iframe
    ,如下所示:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    

希望对您有所帮助……:)

如果您得到的异常是
NoTouchElementException
,可能有以下原因:-

  • 可能您使用的是不正确的
    xpath
    ,因此您需要共享HTML以获得更好的定位器解决方案

  • 当您要查找元素时,它可能不会出现在
    DOM
    上,因此您应该实现
    WebDriverWait
    以等待元素可见并可单击,如下所示:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    
  • 此元素可能位于任何
    iframe
    内。如果是,您需要在查找元素之前切换该
    frame
    iframe
    ,如下所示:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Duty Office")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    


希望对您有所帮助……:)

我已经给出了正确的url,我可以访问该url,但无法单击任何链接或按钮,也无法在页面的文本框中输入数据。您是否尝试使用提供的插件验证Xpath?是的,我验证了仍然面临问题??我给出了正确的url,我可以访问url,但无法单击任何链接或按钮,也无法在页面的文本框中输入数据。您是否尝试使用提供的插件验证Xpath?是的,我已经验证了仍然面临问题??您是否也可以共享链接的HTML?好的,我已经提供了答案,请尝试,如果有问题,请告诉我。:)试试这个“//body/font/font/b/a[2]”你能提供你测试的域和http页面吗?这样我就可以验证你是否也可以分享你链接的HTML?好的,我已经提供了答案,试试看,如果有问题请告诉我。:)试试这个“//body/font/font/b/a[2]“你能提供你正在测试的域和http页面吗?这样我就可以验证Ethanks Saurabh,它成功了。”。但是现在在点击链接后,我在线程“main”org.openqa.selenium.WebDriverException中又遇到一个错误异常:未知错误:脚本没有返回元素引用(会话信息:chrome=44.0.2403.89)@Elaine由于元素过时,会发生这种情况。您可以存储Web元素并执行操作,直到页面未更改为止。一旦页面被更改,请从他们的案例中删除元素的引用,在这种情况下,您需要再次查找元素…:)哦,好的。但是如果下一个页面(点击链接导航到新页面)有同样的问题,那么我可以使用我已经使用过的wait语句,还是需要再次创建一个新的wait语句?在新的页面中,我试图找到一个新元素,但遇到了与第一个页面相同的问题place@Elaine无需再次创建新的等待对象。只需使用已创建的具有相同预期条件的等待对象…:)谢谢,很抱歉给您带来麻烦。正如我前面提到的,导航到新页面时,现在无法识别框架。只识别并点击了第一帧,但没有点击下一帧。谢谢Saurabh,它成功了。但是现在在点击链接后,我在线程“main”org.openqa.selenium.WebDriverException中又遇到一个错误异常:未知错误:脚本没有返回元素引用(会话信息:chrome=44.0.2403.89)@Elaine由于元素过时,会发生这种情况。您可以存储Web元素并执行操作,直到页面未更改为止。一旦页面被更改,请从他们的案例中删除元素的引用,在这种情况下,您需要再次查找元素…:)哦,好的。但是如果下一个页面(点击链接导航到新页面)有同样的问题,那么我可以使用我已经使用过的wait语句,还是需要再次创建一个新的wait语句?在新的页面中,我试图找到一个新元素,但遇到了与第一个页面相同的问题place@Elaine无需再次创建新的等待对象。只需使用已创建的具有相同预期条件的等待对象…:)谢谢,很抱歉给您带来麻烦。正如我前面提到的,导航到新页面时,现在无法识别框架。只识别并单击了第一帧,但未识别和单击下一帧。