ZheniaMagic
ZheniaMagic

Reputation: 119

Access elements on the next page with Selenium

I have a problem with an automatic google search using Selenium. So, I need to paste into google search something like 'JFK annual passengers 2019' and find the answer. The answer is usually shown at the top of the page like that:

I wrote the code to do it and I cannot use it because of the error (no such element exception).

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
def search(link):
   driver = webdriver.Safari()
   driver.get("http://www.google.com")
   elem = driver.find_element_by_name("q")
   elem.clear()
   elem.send_keys(link)
   elem.submit()
   elem1 = driver.find_element_by_class_name('Z0LcW XcVN5d AZCkJd')
   nyam = elem1.get_attribute("data-tts-text")
   print(nyam)
   driver.close()
search('JFK annual passengers 2019')

I think the problem is that I am not looking at the new google page I opened through starting to search.

Help, please!

Upvotes: 1

Views: 231

Answers (1)

ilyankou
ilyankou

Reputation: 1329

Two things. First, when you find an element with 2 or more classes, use find_element_by_css_selector instead. Second, you need to wait before the search results page loads its DOM. You can use python's time library for that.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

import time

def search(link):
   driver = webdriver.Safari()
   driver.get("http://www.google.com")
   elem = driver.find_element_by_name("q")
   elem.clear()
   elem.send_keys(link)
   elem.submit()

   time.sleep(3) # wait 3 seconds
   elem1 = driver.find_element_by_css_selector('.Z0LcW.XcVN5d.AZCkJd')

   nyam = elem1.get_attribute("data-tts-text")
   print(nyam)
   driver.close()


search('JFK annual passengers 2019')

Upvotes: 2

Related Questions