Home > Software design >  How about searching Google with Selenium?
How about searching Google with Selenium?

Time:11-28

I want to search something on Google using Selenium chromedriver and enter it. I can normally do this within the site, but I couldn't type it into google. What code can we use for this?

driver.findElement(By.xpath("//input[@class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");

Fakat olmadı.

CodePudding user response:

The easiest way to do so is via the url when you perform driver.get(...).

Look at the google url for german shepards: https://www.google.com/search?q=german shepards .

If you want to go the google.com and type in the search box you need to perform a better xpath query then by class name. As mentioned by @f1sh this is because google generates the class names (for scope based css). For me the following works for the search bar.

search_bar : WebElement = driver.get_element(By.XPATH, "//input[@title='Search']")

# then you can perform the send_keys
search_bar.send_keys(...)

Good Luck!

CodePudding user response:

I think the problem is not the search text because I tried too many terms and it didn't work. driver.findElement(By.xpath("//input[@class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");  driver.findElement(By.xpath("//input[@class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys(Keys.ENTER);

these are just an example on different sites. I tried id class type as xpath in every way, but it didn't write the text I wrote in the search bar.

  •  Tags:  
  • java
  • Related