我正在尝试使用此按钮
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()
但是我没有收到任何错误,它也没有单击。如何使用python硒在google上单击第一个搜索结果?
答案 0 :(得分:0)
要单击第一个 Google搜索结果而不是visibility_of_element_located()
,您必须为 element_to_be_clickable()
引诱WebDriverWait,然后您可以使用以下任一Locator Strategies:
使用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()
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
答案 1 :(得分:0)
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() {
}
}
答案 2 :(得分:0)
这是一种 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();
}
}
答案 3 :(得分:-1)
由于第一个结果始终是您可以使用的第一个<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()