[英]How do I click a button in a form using Selenium and Python 2.7?
[英]How to find and click on a button on a website with selenium? (Python 2.7 - Debian)
我試圖單擊一個按鈕,以在網站上加載更多數據。 我當然在time.sleep代碼的前后放置了一點時間,但這是行不通的。
此按鈕的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="fa fa-circle-o-notch fa-spin"></i> Loading earlier flights..." class="btn btn-table-action btn-flights-load">Load earlier flights</button>
我嘗試了這個:
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
為什么不起作用?
編輯:
硒版本為:3.5.0
Firefox版本是:52.3.0
我認為問題出在您的按鈕上。 它應該是:
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.