繁体   English   中英

在 selenium python 中访问下一页的元素

[英]Accessing elements on the next page in selenium python

我正在尝试使用 Selenium 在 Python3.5 中编写一个程序,以使用 Firefox webdriver 在 zbigz.com 中自动下载过程。 我的代码如下:

import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException

#magnet link for the purpose of testing
mag = "magnet:?xt=urn:btih:86259d1c8d9dfbe15b6290268231e68d414fed23&dn=The.Big.Bang.Theory.S09E21.HDTV.x264-LOL%5Bettv%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"

def startdriver():
    #starting firefox driver and waiting for 100 seconds
    driver = webdriver.Firefox()
    driver.implicitly_wait(100)
    return driver

def download(driver, url, mg):
    #opening up firefox at url = www.zbigz.com
    driver.get(url)

    try:
        #accessing the required elements on the first page that opens up
        entry_box = driver.find_element_by_xpath('.//*[@id=\'text-link-input\']')
        go_button = driver.find_element_by_id('go-btn')

        #entering magnet link
        entry_box.clear()
        entry_box.send_keys(mg)
        #clicking on the 'Go' button
        go_button.click()

        #accessing the free option
        free_button = driver.find_element_by_id('cloud-free-btn')
        #clicking on the free option
        free_button.click()

        #now comes the next page ('www.zbigz.com/myfiles') where everything goes wrong
        while driver.find_elements_by_tag_name('html') is None:    #waiting for the page to load
            continue

        #this button is what I need to click
        cloud_btn = driver.find_elements_by_xpath('.//*[@id=\'86259d1c8d9dfbe15b6290268231e68d414fed23\']/div[1]')
        #allowing some time so that the download gets cached fully
        time.sleep(60)
        #clicking
        cloud_btn.click()

    except TimeoutException:
        print('Page could not be loaded. Get a better connection!')

if __name__=='__main__':
    #starting driver and downloading
    d = startdriver()
    download(d, zbigz, mag)
    time.sleep(30)
    d.quit()

但是我无法访问下一页上的按钮。 当我运行此代码时,这是我得到的错误:

回溯(最近一次调用):文件“G:/Python/PyCharm Projects/TorrentDownloader.py”,第 88 行,下载(d,zbigz,mag)文件“G:/Python/PyCharm Projects/TorrentDownloader.py”,第 80 行,在下载 cloud_btn.click() AttributeError: 'list' object has no attribute 'click'

我相信我无法访问下一页上的元素。 由于提交方法是 POST,我不能使用driver.get(zbigz+'myfiles')

因此,请提出一种访问随后页面上元素的方法。

WebDriver.find_elements_by_xpath返回元素列表。 如果您只需要一个元素,请改用WebDriver.find_element_by_xpath (无s ):

cloud_btn = driver.find_element_by_xpath(".//*[@id='86259d1c8d9dfbe15b6290268231e68d414fed23']/div[1]")

顺便说一句,使用".."字符串文字,你不需要在里面转义'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM