【问题标题】:Unable to click on google's search result through selenium web driver (Java)无法通过 selenium 网络驱动程序(Java)点击 google 的搜索结果
【发布时间】:2016-09-01 07:55:45
【问题描述】:

我正在尝试在谷歌上搜索一些东西,让我们说“测试”。所以我想点击第一个搜索结果,但无法做到。请在下面找到我的代码。我尝试过使用 xpath、class 和 cssSelector。请帮忙。:

/**
 * @param args
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://www.google.co.in/");

    driver.findElement(By.id("lst-ib")).sendKeys("Testing" + Keys.ENTER);
    Thread.sleep(5);

    driver.findElement(
            By.className("r"))
            .click();
}

请同时查找 Inspect Element 结果:

软件测试 - 维基百科,免费的百科全书

【问题讨论】:

标签: selenium-webdriver webdriver


【解决方案1】:

您应该尝试使用WebDriverWait 等到元素可见并启用点击如下:-

driver.get("https://www.google.co.in/");

driver.findElement(By.id("lst-ib")).sendKeys("Testing" + Keys.ENTER);

new WebDriverWait(driver, 10)
                .until(ExpectedConditions.elementToBeClickable(By
                        .linkText("Software testing - Wikipedia, the free encyclopedia"))).click();

【讨论】:

    【解决方案2】:

    尝试一次给出链接的位置而不是标题

    xpath= //h3[@class='r']/a
    

    如果您只想点击第一个链接,那么上面的路径就可以了。

    如果您想点击特定元素,请尝试构建返回特定元素的 xpath。例如点击维基百科链接

    xapth=//a[@href='https://en.wikipedia.org/wiki/Software_testing']
    

    你也可以试试

    1.collecting all links into List and 
    2. iterating one by one
    3. By using getText get text and verify required one
    4. once condition satisfy then click on it.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多