Java selenium how to open a link from google search results?

坚强是说给别人听的谎言 提交于 2021-02-05 08:25:08

问题


I am new to automation testing in Selenium and i am doing some basic automation testing such as searching for something in Google and then clicking on a link which is required from the search results.

The code below, which i have produced works up until i get to the testing method. I am unable to select a link from the Google search page but i am not being shown any errors on my console. So i setup a thread on this particular line and it mentioned it could find the link name however the link name is used in the html code as i have checked on Google inspect.

Am i missing something obvious? I am relatively new to Selenium so any help is appreciated. Also i have tried mirroring some code from this users response "How to click a link by text in Selenium web driver java" but no luck!

Thanks

    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();


}
}

回答1:


Once you obtain the search results for the text BBC on Google Home Page next to click() on the link containing the text BBC - Home you can use the following code block :

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();
}



回答2:


You can use this code:

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();
}


来源:https://stackoverflow.com/questions/49985758/java-selenium-how-to-open-a-link-from-google-search-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!