I am using Google search for finding the element, as it seems easier to navigate than IMDB.
import selenium.webdriver as webdriver
print("This program finds the imdb rating of a movie or TV series!!!")
def get_results(search_term):
url="https://www.google.com"
browser=webdriver.Safari()
browser.get(url)
search_box= browser.find_element_by_id("lst-ib")
search_box.send_keys(search_term)
search_box.submit()
links = browser.find_element_by_id("//div[@class = 'slp f']/text()")#this line is problematic, should i use xpath?how?
print(links)
search_key=input("Enter the movie name : ")
get_results("what is the imdb rating of "+search_key)
and here is the error...
This program finds the imdb rating of a movie or TV series!!!
Enter the movie name : inception
Traceback (most recent call last):
File "web1.py", line 15, in <module>
get_results("what is the imdb rating of "+search_key)
File "web1.py", line 10, in get_results
links = browser.find_element_by_id("//div[@class = 'slp f']/text()")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 353, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 957, in find_element
'value': value})['value']
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.
Please be specific. Thank you!
I have a little bit changed your code and it works. Added explicit wait and changed selector.
import selenium.webdriver as webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
print("This program finds the imdb rating of a movie or TV series!!!")
def get_results(search_term):
url = "https://www.google.com"
browser = webdriver.Safari()
browser.get(url)
search_box= browser.find_element_by_id("lst-ib")
search_box.send_keys(search_term)
search_box.submit()
rating = WebDriverWait(browser, 5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="rso"]/div/div/div[1]/div/div/div[1]/div/div[2]')))
print(rating.text)
links = browser.find_elements_by_xpath('//h3/a')
first_link = links[0].get_attribute('href')
print(first_link)
browser.quit()
search_key=input("Enter the movie name : ")
get_results("what is the imdb rating of "+search_key)
Hope, this will help you.