【问题标题】:python selenium find element by xcodepython selenium通过xcode查找元素
【发布时间】:2018-06-18 17:57:19
【问题描述】:
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()
    href=""
    links=browser.get_element_by_class("//slp f//a")
    print(links)

    browser.close()

search_key=input("Enter the movie name : ")
get_results("what is the imdb rating of "+search_key)

如何将网页中的特定行打印到终端? 我可以使用find_element_by_xcode()get_element_by_class() 吗?

【问题讨论】:

    标签: python selenium web-crawler


    【解决方案1】:

    您可以使用CSS SelectorxPath 等。例如您有此搜索results 并且您想打印评分:

    然后你可以像这样定位元素:

    links = browser.find_elements_by_xpath("//div[@class = 'slp f']/text()") # find all elements with given selector
    for link in links
       print(link) # prints in the terminal current link value
                   # for example ' Rating: 8,4/10 - ‎432,408 votes'
    

    更多关于如何使用Python 定位元素的信息,您将在documentation 中获得。

    【讨论】:

    • selenium.common.exceptions.NoSuchElementException:消息:使用给定的搜索参数无法在页面上找到元素。更新此程序后出现此错误,我尝试使用 #div 而不是 div 但我得到了相同的结果
    • 我已经更新了我的答案,请看一下。问题是,结果页面上有许多相同的元素。因此,您可以找到所有这些,然后将它们全部打印出来,或者选择您需要的评级。
    • 浏览器在执行循环之前突然关闭
    • 更新了我的答案,请看一下
    • 仍然做同样的事情,在循环之前做所有事情,然后关闭浏览器并终止程序。
    猜你喜欢
    • 2020-11-29
    • 2018-12-24
    • 2020-10-30
    • 1970-01-01
    • 2016-03-12
    • 2017-08-08
    • 1970-01-01
    • 2020-12-07
    • 2015-12-25
    相关资源
    最近更新 更多