【问题标题】:I want to print the IMDB rating of a movie/series to the terminal after the completion of automation我想在自动化完成后将电影/系列的 IMDB 评级打印到终端
【发布时间】:2018-07-10 14:29:04
【问题描述】:

我正在使用 Google 搜索来查找元素,因为它似乎比 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)

这是错误...

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.

请具体说明。谢谢!

【问题讨论】:

  • 您可能需要将范围缩小到出现错误的部分,以便人们更容易为您提供帮助。事实上,对行号和源文件的引用可能会为正在发生的事情提供更多上下文。为minimal, complete and verifiable 示例提供其他相关类。
  • 请修正缩进以获得经过充分研究的答案。

标签: html css selenium web web-crawler


【解决方案1】:

我对您的代码进行了一些更改,并且可以正常工作。添加了显式等待并更改了选择器。

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)

希望对你有所帮助。

【讨论】:

  • 谢谢,它完美无缺。您能否详细说明一下直到()中发生的事情。再次感谢!
  • 这会在抛出 TimeoutException 之前等待最多 5 秒,除非它找到要在 5 秒内返回的元素。默认情况下,WebDriverWait 每 500 毫秒调用一次 ExpectedCondition,直到它成功返回。对于 ExpectedCondition 类型的成功返回是 Boolean 对所有其他 ExpectedCondition 类型返回 true 或非 null 返回值。
  • 我为你感到高兴。请你选择正确的答案好吗?此外,您可以在此处阅读有关 Selenium 等待的更多信息:selenium-python.readthedocs.io/waits.html
  • 再次感谢您的帮助。忘记选择正确答案。错误已更正。
  • 从最近几天开始,我实际上一直在为导航而苦苦挣扎。您能解释一下如何使用 XPath 和其他方法进行导航吗?
猜你喜欢
  • 2014-12-15
  • 2013-01-08
  • 2020-09-11
  • 2011-04-12
  • 1970-01-01
  • 2014-01-04
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多