【问题标题】:Checking the clickability of an element in selenium using python使用python检查硒中元素的可点击性
【发布时间】:2016-05-17 08:13:07
【问题描述】:

我一直在尝试编写一个脚本,该脚本将为我提供此页面上所有剧集的链接:- http://www.funimation.com/shows/assassination-classroom/videos/episodes

正如您所见,链接可以在“外部 HTML”中看到,我将 selenium 和 PhantomJS 与 python 一起使用。

链接示例:http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time

但是,我似乎无法正确编写代码。我确实对我想做的事情有一个基本的想法。这是过程:-

1.) 复制第一页的外部 HTML,然后将其保存为“Source_html”文件。

2.) 在此文件中查找链接。

3.) 移至下一页以查看其余视频及其链接。

4.) 重复步骤 2。

这就是我的代码的样子:

from selenium import webdriver
from selenium import selenium
from bs4 import BeautifulSoup
import time

# ---------------------------------------------------------------------------------------------

driver = webdriver.PhantomJS()
driver.get('http://www.funimation.com/shows/assassination-classroom/videos/episodes')

elem = driver.find_element_by_xpath("//*")


source_code = elem.get_attribute("outerHTML")
f = open('source_code.html', 'w')
f.write(source_code.encode('utf-8'))
f.close()

print 'Links On First Page Are : \n'
soup = BeautifulSoup('source_code.html')
subtitles = soup.find_all('div',{'class':'popup-heading'})
official = 'something'

for official in subtitles:
        x = official.findAll('a')
        for a in x:
            print a['href']


sbtn = driver.find_element_by_link_text(">"):
print sbtn

print 'Entering The Loop Now'
for driver.find_element_by_link_text(">"):
    sbtn.click()
    time.sleep(3)
    elem = driver.find_element_by_xpath("//*")
    source_code = elem.get_attribute("outerHTML")
    f = open('source_code1.html', 'w')
    f.write(source_code.encode('utf-8'))
    f.close()

我已经知道的事情:-

soup = BeautifulSoup('source_code.html') 不起作用,因为我需要通过 python 打开这个文件,然后将其输入 BS。我能做到的。

official 变量实际上并没有做任何事情。只是帮我开始循环。

for driver.find_element_by_link_text(">"): 

现在,这就是我需要以某种方式解决的问题。我不确定如何检查这个东西是否仍然可以点击。如果是,则进入下一页,获取链接,再次单击此链接以转到第 3 页并重复该过程。

任何帮助将不胜感激。

【问题讨论】:

    标签: python python-2.7 selenium phantomjs


    【解决方案1】:

    基本上,我使用webelement.is_displayed() 来检查它是否可点击。

    isLinkDisplay = driver.find_element_by_link_text(">").is_displayed()
    

    【讨论】:

      【解决方案2】:

      您根本不需要在这里使用BeautifulSoup。只需通过selenium 获取所有链接。仅当> 链接可见时,才进入下一页。这是完整的实现,包括收集链接、必要的等待。它应该适用于任何页数:

      import time
      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      
      driver = webdriver.PhantomJS()
      driver.get("http://www.funimation.com/shows/assassination-classroom/videos/episodes")
      
      wait = WebDriverWait(driver, 10)
      
      links = []
      while True:
          # wait for the page to load
          wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.item-title")))
      
          # wait until the loading circle becomes invisible
          wait.until(EC.invisibility_of_element_located((By.ID, "loadingCircle")))
      
          links.extend([link.get_attribute("href") for link in driver.find_elements_by_css_selector("a.item-title")])
      
          print("Parsing page number #" + driver.find_element_by_css_selector("a.jp-current").text)
      
          # click next
          next_link = driver.find_element_by_css_selector("a.next")
          if not next_link.is_displayed():
              break
      
          next_link.click()
          time.sleep(1)  # hardcoded delay
      
      print(len(links))
      print(links)
      

      对于问题 URL 中提到的内容,它会打印:

      Parsing page number #1
      Parsing page number #2
      93
      ['http://www.funimation.com/shows/assassination-classroom/videos/official/assassination-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/assassination-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/assassination-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/baseball-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/baseball-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/baseball-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/grown-up-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/grown-up-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/grown-up-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/assembly-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/assembly-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/assembly-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/test-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/test-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/test-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time1st-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time1st-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time1st-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/school-trip-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/l-and-r-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/l-and-r-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/l-and-r-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/transfer-student-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/ball-game-tournament-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/ball-game-tournament-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/ball-game-tournament-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/talent-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/talent-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/talent-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/vision-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/vision-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/vision-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/end-of-term-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/end-of-term-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/end-of-term-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/schools-out1st-term', 'http://www.funimation.com/shows/assassination-classroom/videos/official/schools-out1st-term', 'http://www.funimation.com/shows/assassination-classroom/videos/official/schools-out1st-term', 'http://www.funimation.com/shows/assassination-classroom/videos/official/island-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/island-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/island-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/action-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/action-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/action-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/pandemonium-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/pandemonium-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/pandemonium-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time2nd-period', 'http://www.funimation.com/shows/assassination-classroom/videos/official/karma-time2nd-period', 'http://www.funimation.com/shows/deadman-wonderland', 'http://www.funimation.com/shows/deadman-wonderland', 'http://www.funimation.com/shows/riddle-story-of-devil', 'http://www.funimation.com/shows/riddle-story-of-devil', 'http://www.funimation.com/shows/soul-eater', 'http://www.funimation.com/shows/soul-eater', 'http://www.funimation.com/shows/assassination-classroom/videos/official/xx-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/xx-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/xx-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/nagisa-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/nagisa-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/nagisa-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/summer-festival-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/summer-festival-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/summer-festival-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/kaede-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/kaede-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/kaede-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/itona-horibe-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/itona-horibe-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/itona-horibe-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/spinning-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/spinning-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/spinning-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/leader-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/leader-time', 'http://www.funimation.com/shows/assassination-classroom/videos/official/leader-time', 'http://www.funimation.com/shows/deadman-wonderland', 'http://www.funimation.com/shows/deadman-wonderland', 'http://www.funimation.com/shows/riddle-story-of-devil', 'http://www.funimation.com/shows/riddle-story-of-devil', 'http://www.funimation.com/shows/soul-eater', 'http://www.funimation.com/shows/soul-eater']
      

      【讨论】:

      • 天哪,我不能为此感谢你。不过,我需要先了解您的代码。你能解释一下你的links.extend([link.get_attribute("href") for link in driver.find_elements_by_css_selector("a.item-title")]) 行吗?
      • @user2408212 当然,这是收集链接的代码的“心脏”。在这里,我们使用item-title 类定位a 元素并获取href 属性列表,然后将其放入links 列表中。希望对您有所帮助。
      • 你的代码就像一个魅力。现在,我必须弄清楚如何删除额外的链接。如果你看到我的代码,for official in subtitles: 就是让我得到我需要的链接(但重复条目)。
      • @user2408212 啊,当然,试试div.popup-heading a 而不是a.item-title CSS 选择器。
      • 给出“无”。不过告诉我一件事。我正在考虑检查特定字符串的网址。如果 URL 与该特定字符串匹配,则保留它。听起来怎么样?
      猜你喜欢
      • 2015-01-12
      • 2019-01-26
      • 2018-02-04
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2016-12-27
      • 2021-03-05
      相关资源
      最近更新 更多