【问题标题】:Java selenium how to open a link from google search results?Java selenium 如何从谷歌搜索结果中打开链接?
【发布时间】:2018-10-03 18:36:14
【问题描述】:

我是 Selenium 自动化测试的新手,我正在做一些基本的自动化测试,例如在 Google 中搜索某些内容,然后单击搜索结果中所需的链接。

我制作的下面的代码一直有效,直到我得到测试方法。我无法从 Google 搜索页面中选择链接,但我的控制台上没有显示任何错误。所以我在这个特定的行上设置了一个线程,它提到它可以找到链接名称,但是链接名称在 html 代码中使用,因为我已经检查过 Google 检查。

我是否遗漏了一些明显的东西?我对 Selenium 比较陌生,因此感谢您提供任何帮助。我也尝试从这个用户响应中镜像一些代码 "How to click a link by text in Selenium web driver java" 但没有运气!

谢谢

    package com.demo.testcases;






import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;





public class MyFirstTestScript {

private static WebDriver driver;


public static void main (String[] args)  {

    SetUp();
    testing();



}

// TODO Auto-generated method stub


@setup

    public static void SetUp () {

    driver = new FirefoxDriver();
    driver.get("http://www.google.co.uk");
    System.setProperty("webdriver.gecko.driver", "usr/local/bin/geckodriver");
    driver.findElement(By.name("q")).sendKeys("BBC" + Keys.ENTER);
}   
@Test
        public static void testing()  {

    driver.findElement(By.partialLinkText("BBC - Home")).click();


}
}

【问题讨论】:

  • 这是另一种方法,但您可以将搜索请求构建到您传递给 driver.get 的 URL 中。
  • 尝试使用 By.linkText 而不是 PartialLinkText
  • @camel-man 我最初尝试过这种方法,但没有奏效

标签: java selenium selenium-webdriver web automation


【解决方案1】:

一旦您在 Google 主页 上在包含文本 BBC - Home 的链接上的 click() 旁边获得文本 BBC 的搜索结果后strong> 你可以使用下面的代码块:

List <WebElement> my_list = driver.findElements(By.xpath("//div[@id='rso']//div[@class='rc']/h3[@class='r']/a"));
for (WebElement item:my_list)
{
    if(item.getAttribute("innerHTML").contains("BBC - Home"))
    item.click();
}

【讨论】:

  • 感谢您的帮助,您是否测试过这段代码?我现在刚刚尝试过,但没有运气!我之前使用了这个“”方法,但是我没有使用 x.path 方法,而是使用了 By.linktext 方法,但它没有执行。我现在已经尝试了上面的代码,但仍然没有运气。
【解决方案2】:

你可以使用这个代码:

public class MyFirstTestScript {

private static WebDriver driver;
private static WebDriverWait wait;

public static void main (String[] args)  {

    SetUp();
    testing();
}

@setup
public static void SetUp () {
    System.setProperty("webdriver.gecko.driver", "usr/local/bin/geckodriver");
    driver = new FirefoxDriver();
    wait = new WebDriverWait(driver,50); 
    driver.manage().window().maximize();
    driver.get("http://www.google.co.uk");
    wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
    driver.findElement(By.name("q")).sendKeys("BBC" + Keys.ENTER);
} 


@Test
public static void testing(){
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.linkText("BBC - Homepage"))));
    driver.findElement(By.linkText("BBC - Homepage")).click();
}

【讨论】:

  • 感谢您的回复。我刚刚尝试了这段代码,但它仍然无法正常工作。你有没有偶然测试过这段代码?
  • 它一直工作到搜索结果点,但是它没有点击指定的链接。从控制台我收到此错误“线程中的异常”主“org.openga.selenium.NoSuchElementException:无法找到元素:BBC - Home”
  • 我尝试搜索 Google 而不是 BBC,但我得到了同样的错误,它找不到元素“Google”?
  • BBC-首页是得到结果后弹出的第一个链接吗?
  • 可能是你的脚本需要向下滚动搜索页面
猜你喜欢
  • 2022-01-18
  • 2021-08-23
  • 1970-01-01
  • 2019-01-29
  • 2018-07-06
  • 2015-07-27
  • 1970-01-01
  • 2012-02-03
  • 2016-05-14
相关资源
最近更新 更多