0

I'm trying to create program in selenium as following. Currently worked fine for 1 and 2, but 3 is not working. Anyone know the issue?

  1. access to google
  2. search some word
  3. get the top hit website
  4. access that website
  5. get h2 element in that website
  6. get screenshot of that website
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()

#Open Google
driver.get('https://www.google.com/')
time.sleep(2)

#search some word
search_box = driver.find_element("name", "q")
search_box.send_keys('スタビジ')
search_box.submit()
time.sleep(2)

#get the top website URL
g = driver.find_element(By.NAME, "g")[0]
r = g.find_element(By.NAME, "r")
r.click()

#get h2 element
for h2 in driver.find_element(By.TAG_NAME, ‘h2’):
    print(h2.text)

#get screenshot
driver.maximize_window()
driver.save_screenshot('スタビジ.png')

#close browser
driver.quit()[![enter image description here](https://i.stack.imgur.com/Kqq7I.jpg)](https://i.stack.imgur.com/Kqq7I.jpg)

currently, im having this issue enter image description here

I'm expecting following procedure

  • access to google
  • search some word
  • get the top hit website
  • access that website
  • get h2 element in that website
  • get screenshot of that website
buhtz
  • 10,774
  • 18
  • 76
  • 149
Ota Yosuke
  • 13
  • 2

2 Answers2

1

The line: g = driver.find_element(By.NAME, "g")[0] is problematic. From my browser's DevTools, I can see that there are no elements with a name of "g". What you probably meant was to do g = driver.find_element(By.CLASS_NAME, "g"). You can then proceed to extract out the link as follows: g.find_element(By.TAG_NAME, "a").

1

To identify the <g> element you can use either of the following locator strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "svg g")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//*[name()='svg']//*[name()='g']")
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352