简体   繁体   中英

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.

This button html code looks like this :

<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

Firefox version is: 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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