简体   繁体   English

如何在含硒的网站上找到并单击按钮? (Python 2.7-Debian)

[英]How to find and click on a button on a website with selenium? (Python 2.7 - Debian)

I am trying to click on a button, to load more dataes on a website. 我试图单击一个按钮,以在网站上加载更多数据。 I put before and after a time.sleep code part of course to wait a little bit time, but it isn't work. 我当然在time.sleep代码的前后放置了一点时间,但这是行不通的。

This button html code looks like this : 此按钮的html代码如下所示:

<button data-mode="arrivals" data-page="-1" data-timestamp="1506584673.386" ng-click="loadMoreFlights($event)" data-current-page="1" data-loading-text="<i class=&quot;fa fa-circle-o-notch fa-spin&quot;></i> Loading earlier flights..." class="btn btn-table-action btn-flights-load">Load earlier flights</button>

And I tried this: 我尝试了这个:

def scrape(urls):
    browser = webdriver.Firefox()
    datatable=[]
    for url in urls:
        browser.get(url)
        time.sleep(5)
        driver.find_element_by_xpath("//a[contains(.,'Load earlier flights')]")
        time.sleep(5)
        html = browser.page_source
        soup=BeautifulSoup(html,"html.parser")
        table = soup.find('table', { "class" : "table table-condensed table-hover data-table m-n-t-15" })
        soup2=BeautifulSoup(html,"html.parser")
        name = soup2.h2.string
        soup3=BeautifulSoup(html,"html.parser")
        name2 = soup3.h1.string
        soup4=BeautifulSoup(html,"html.parser")
        name3 = soup4.h3.string
        name4 = datetime.now()

        for record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"):
            temp_data = []
            temp_data.append(name4)
            temp_data.append(name)
            temp_data.append(name2)    
            temp_data.append(name3)    
            for data in record.find_all("td"):
                temp_data.append(data.text.encode('latin-1'))
            newlist = filter(None, temp_data)
            datatable.append(newlist)

    time.sleep(10) 
    browser.close()
    return datatable 

Why is it not working? 为什么不起作用?

EDIT: 编辑:

Selenium version is : 3.5.0 硒版本为:3.5.0

Firefox version is: 52.3.0 Firefox版本是:52.3.0

I think the problem is the path to your button. 我认为问题出在您的按钮上。 It should be: 它应该是:

driver.find_element_by_xpath('//button[contains(text(), "Load earlier flights")]').click()

试试这个代码,它正在工作

      driver.find_element_by_xpath('//button[contains(@class,'btn btn-table-action btn-flights-load')][contains(text(),'Load earlier flights')]').click();

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

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