简体   繁体   English

如何使用 Selenium 和 Python 在 google 上点击第一个 google 搜索结果

[英]How to click on the first google search result on google using Selenium and Python

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?如何使用 python selenium 在 google 上点击第一个搜索结果?

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 :要单击第一个Google 搜索结果而不是visibility_of_element_located() ,您必须为element_to_be_clickable()诱导WebDriverWait ,并且可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用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 :使用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

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):由于第一个结果始终是您可以使用的第一个<h3> tag (此示例转到 google.com 输入“test”并单击第一个结果):

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

Here is a Java way.这是Java方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python selenium点击第一个谷歌搜索结果? - How to click on the first google search result with python selenium? 如何使用 selenium python 在谷歌上点击第一个结果 - How to click on the first result on google using selenium python 如何使用 Python Selenium 点击第一个 Yahoo 搜索结果 - How to click on the first Yahoo search result using Python Selenium 使用 python 进行 Google 搜索并抓取第一个结果 - Google search using python and scrape first result Selenium Python Google搜索,从结果中单击选择的URL - Selenium Python Google search, click chosen URL from result 如何使用selenium-webdriver单击Google搜索结果 - How can i click Google search result with selenium-webdriver 如何使用 selenium 网络驱动程序点击谷歌搜索结果列表中的第二个链接 - How to click on the second link in google search result list using selenium web driver 如何在 Python 中使用 Selenium 在 Google 上搜索? - How to search on Google with Selenium in Python? 如何使用 python selenium 在动态页面上单击第一个结果? - How to click on the first result on a dynamic page using python selenium? 如何使用 Selenium 和 Python 提取第一个搜索结果的 href 属性 - How to extract the href attribute of the first search result using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM