【问题标题】:Locating WebElement using different locators(NoSuchElementException)使用不同的定位器定位 WebElement (NoSuchElementException)
【发布时间】:2019-12-30 11:47:54
【问题描述】:

我在使用不同的定位器定位 WebElement 时遇到问题。在下面的 html 标记中,我尝试使用 linkTextxpathclassname 等不同的定位器找到“写评论”WebElement,但仍然得到 NoSuchElementException

-->url https://www.tripadvisor.in/-->search for Club Mahindra-->click on Club Mahindra-->click on write a review.

    <a href="/UserReview-g641714-d1156207-Club_Mahindra_Madikeri_Coorg- 
    Madikeri_Kodagu_Coorg_Karnataka.html" target="_blank" class="ui_button 
    primary">Write a review</a>

使用的定位器

  • By.xpath("//*[@id="component_12"]/div/div[2]/div/div[2]/div/div[1]/a")
  • By.xpath("//a[@href='/UserReview-g641714-d1156207- Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html']")
  • By.className("ui_button primary")
  • By.linkText("Write a review")

我真的很困惑。我做错了什么?

【问题讨论】:

  • 你能分享你解决这个问题的尝试吗?您使用过哪些 xpath、类名等?
  • By.xpath("//*[@id="component_12"]/div/div[2]/div/div[2]/div/div[1]/a") 和By.xpath("//a[@href='/UserReview-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html']") , By.className("ui_button primary"), By.linkText("写评论")
  • 您是否使用了任何显式或隐式等待?可能很简单,当您尝试单击按钮时页面未加载。
  • 是的,我尝试了等待...也尝试了操作类,但它仍然显示 NoSuchElementException @Jonah

标签: java eclipse selenium selenium-webdriver testng


【解决方案1】:

我已经厌倦了分析和实施相同的方法。以下是我的发现:

-> 应用程序等待时间更长,因为有很多适用于页面的动态负载。

-> 需要实现适当的等待

-> 检查所有页面是否都在同一个选项卡中打开,或者单击每个链接是否重定向到新选项卡,如果是,则我们必须切换到该特定窗口。

-> 下面的代码对我来说就像专业人士一样。

    driver.get("https://www.tripadvisor.in/");
    WebDriverWait wait = new WebDriverWait(driver, 120);
    WebElement ele1 =  
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Where to?']")));
    ele1.click();

    WebElement ele2= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@placeholder='Where to?']")));
    ele2.sendKeys("club mahindra, india");

    WebElement ele3= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Search for ')]")));
    ele3.click();
    WebElement ele4= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Club Mahindra Madikeri, Coorg')]")));
    ele4.click();   //this click leads to a new tab 

    Set<String> winHandles = driver.getWindowHandles();  

    for(String str : winHandles) {
        driver.switchTo().window(str);
    }
    System.out.println(driver.getTitle());

    WebElement ele;
    int i=1;
    while(true) {
        try {
         ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Write a review']")));
         break;
        }catch(Exception e) {
            System.out.print(i++);
        }
    }
    System.out.println();
    Actions action = new Actions(driver);
    action.moveToElement(ele);
    ele.click();
    System.out.println("Clicked on the 'Write a review button'");

【讨论】:

    【解决方案2】:

    你可以试试

      //a[contains(text(),'Write a review')]
    

    【讨论】:

    • 我尝试使用 xpath 但仍然卡在 NoSuchElementException 。它也不起作用
    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2013-11-18
    • 2015-12-08
    • 2015-10-19
    • 2018-07-06
    相关资源
    最近更新 更多