Unable to locate an element using xpath error in selenium-java

前端 未结 4 890
忘掉有多难
忘掉有多难 2021-01-17 06:23

This is the code I am trying to execute

public WebDriver createPart() {

    try {
        driver.findElement(By.id(\"username\")).sendKeys(\"502409373\");
          


        
相关标签:
4条回答
  • 2021-01-17 06:53

    your parentWindowHandler should not be correct, you fetch it at wrong time point.

    Try move below code line to the first line in the try block

    try {
       String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
    
    0 讨论(0)
  • 2021-01-17 06:56

    Change visibilityOfElementLocated instead of elementToBeClickable. You can directly find the webeelement, and then click on it as shown below:

    element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']")));
    
    0 讨论(0)
  • 2021-01-17 07:02

    Once switched to parent window, try to refresh the page then find the element as given below. It may solve your issue.

        driver.switchTo().window(parentWindowHandler);
        driver.navigate().refresh();
    
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 60);
        element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Part Details']")));
        element.click();
    
    0 讨论(0)
  • 2021-01-17 07:05

    To invoke click() on the link with text as Part Details you need to induce WebDriverWait and invoke click() as follows :

    • linkText :

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

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.tablink[title='Part Details']"))).click();
      
    • xpath :

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='tablink' and @title='Part Details']"))).click();
      
    0 讨论(0)
提交回复
热议问题