【问题标题】:How to access text inside div tags using Selenium in Python?如何在 Python 中使用 Selenium 访问 div 标签内的文本?
【发布时间】:2019-12-21 13:33:23
【问题描述】:

我正在尝试使用 Selenium 在 Python 中制作一个程序,该程序会打印出来自 https://www.brainyquote.com/quote_of_the_day 的引号

编辑: 我能够像这样访问引用和相关作者:

    authors = driver.find_elements_by_css_selector("""div.col-xs-4.col-md-4 a[title="view author"]""") 
for quote,author in zip(quotes,authors): 
        print('Quote: ', quote.text) 
        print('Author: ', author.text)

无法以类似方式组合主题。正在做

total_topics = driver.find_elements_by_css_selector("""div.col-xs-4.col-md-4 a.qkw-btn.btn.btn-xs.oncl_list_kc""")

会列出不受欢迎的列表

之前我使用的是 Beautiful Soup,它完美地完成了这项工作,除了请求库只能访问静态网站这一事实。但是,我希望能够连续滚动网站以继续访问新报价。为此,我正在尝试使用 Selenium。

这就是我使用 Soup 的方式:

for quote_data in soup.find_all('div', class_='col-xs-4 col-md-4'):  
       quote = quote_data.find('a',title='view quote').text 
       print('Quote: ',quote)

但是,我无法使用 Selenium 找到相同的内容。 我在 Selenium 中用于基本测试的代码:

driver.maximize_window() 
driver.get('https://www.brainyquote.com/quote_of_the_day') 
elem = driver.find_element_by_tag_name("body")

elem.send_keys(Keys.PAGE_DOWN) 
time.sleep(0.2) 

quote = driver.find_element_by_xpath('//div[@title="view quote"]')

我也尝试过 CSS 选择器

print(driver.find_element_by_css_selector('div.col-xs-4 col-md-4')

后者给出了 NoSuchElementFound 异常,而前者根本没有给出任何输出。我很想得到一些关于我哪里出错以及如何解决这个问题的提示。

谢谢!

【问题讨论】:

    标签: python selenium css-selectors


    【解决方案1】:
    quotes = driver.find_elements_by_xpath('//a[@title="view quote"]')
    

    首先滚动到底部

    【讨论】:

      【解决方案2】:

      您可能需要编写某种循环来滚动并单击引号链接,直到找不到更多元素。以下是我将如何做到这一点的概要:

      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      
      driver.get('https://www.brainyquote.com/quote_of_the_day') 
      
      while True:
      
          # wait for all quote elements to appear
          quote_links = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//a[@title='view quote']")))
      
          # todo - need to check for the end condition. page has infinite scrolling
          # break
      
          # iterate the quote elements until we reach the end of this list
          for quote_link in quote_links:
              quote_link.click()
              driver.back()
      
              # now quote_links has gone stale because we are on a different page
              quote_links = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//a[@title='view quote']")))
      

      上面的代码进入一个循环,搜索页面上所有的“查看更多”引用链接。然后,我们迭代链接列表并单击每个链接。此时quote_links列表中的元素由于页面不再存在而变得陈旧,所以我们在点击另一个链接之前重新找到WebDriverWait的元素。

      这只是一个粗略的大纲,需要做一些额外的工作来确定页面无限滚动的最终情况,并且您需要编写在报价页面本身上执行的操作,但希望你在这里看到了这个想法。

      【讨论】:

      • 查看帖子中的编辑。我如何将主题与相应的引用和作者联系起来?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多