Skip to main content
 首页 » 编程设计

python之使用python检查 Selenium 中元素的可点击性

2024年10月24日9lhb25

我一直在尝试编写一个脚本,它将为我提供指向此页面上出现的剧集的所有链接:- 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 页并重复该过程。

如有任何帮助,我们将不胜感激。

请您参考如下方法:

你根本不需要在这里使用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']