python selenium通过xcode查找元素

时间:2018-06-18 17:57:20

标签: python selenium web-crawler

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()吗?

1 个答案:

答案 0 :(得分:0)

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

image

然后你可以找到这样的元素:

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 var b = new Array(); for (var n = 0; n < 10; n++) { b[n] = document.getElementById("b" + n); } 查找元素的详细信息。