Reputation: 115
I am trying to click on it using this
result = WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/a/h3")))
result.click()
But I am not getting any errors and it doesn't click as well. How to click on the first search result on google with python selenium?
Upvotes: 1
Views: 7670
Reputation: 81
Here is a Java way.
import com.automation.DriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class GSearch {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = DriverManager.getChromeDriver();
driver.get("https://www.google.com/");
//Set search parameters
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("Selenium Testing");
//Click on Google search button
driver.findElement(By.xpath("//div[@class='FPdoLc tfB0Bf']//input[@class='gNO89b']")).click();
//wait sometimes
Thread.sleep(2000);
//click google first search result
driver.findElement(By.xpath("//div[@id='rso']/child::div[@class='hlcw0c'][1]/div[1]/div[@class='tF2Cxc']/div[@class='yuRUbf']/a")).click();
//wait sometimes
Thread.sleep(4000);
//close the browser
driver.close();
}
}
Upvotes: 0
Reputation: 978
I can share java code (logic is same ) what I am doing is open chrome and type as"**Testing**" and store all results and and picking the "**Testing Types**" from list
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List;
public class googleresults {
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver","/Users/justinlambert/Documents/Selenium_Projects/day2/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Testing");
driver.findElement(By.id("block-517e9e97-d25b-41b1-8108-463e0137df8c")).sendKeys("text");
List<WebElement> results=driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
System.out.println(results.size());
for (int i=0;i<results.size();i++)
{
if (results.get(i).getText().contains("testing types"))
{
results.get(i).click();
break;
}
}
}
@Test
public void testresults() {
}
}
Upvotes: 0
Reputation: 193088
To click on the first Google Search Result instead of visibility_of_element_located()
you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
:
driver.get("http://www.google.com")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
element.send_keys("selenium")
element.send_keys(Keys.ENTER)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "div#search div.g > div.rc > div.r > a"))).click()
Using XPATH
:
driver.get("http://www.google.com")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
element.send_keys("selenium")
element.send_keys(Keys.ENTER)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='search']//div[@class='g']/div[@class='rc']/div[@class='r']/a"))).click()
Note : You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 2
Reputation: 1769
Since the first result is always the first <h3> tag
you can use (this example goes to google.com enters "test" and clicks on the first result):
browser.get('https://www.google.com/')
time.sleep(3)
google = WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')))
google.send_keys('test')
time.sleep(1)
google.send_keys(Keys.RETURN)
time.sleep(3)
result = browser.find_element_by_tag_name('h3')
result.click()
Upvotes: 0