2015-01-09 1267 views
4

我目前有一個腳本將我登錄到一個網站,如果它沒有被點擊,我想讓它點擊網站上的一個按鈕。下面是按鈕的信息:python點擊網頁上的一個按鈕

當按鈕已經被激活:

<p class="toast_btn"> 
    <a class="button grey toast track-click active" data-user-avatar="https://dwebsite.net/picture.jpg" data-checkin-id="123456789" data-href=":feed/toast" data-track="activity_feed" href="#"> 

當按鈕未激活:

<p class="toast_btn"> 
    <a class="button grey toast track-click" data-user-avatar="https://dwebsite.net/picture.jpg" data-checkin-id="123456789" data-href=":feed/toast" data-track="activity_feed" href="#"> 

我只想找點擊它時class="button grey toast track-click"

這樣做的最佳方法是什麼?我目前使用urllib2並機械化登錄並檢查幾個表單。謝謝!

+0

你」我希望能夠像BeautifulSoup一樣在HTML中工作:http://www.crummy.com/softwa re/BeautifulSoup/bs4/doc/ – dylrei 2015-01-09 21:15:17

回答

6

當我比較兩個標籤時,我發現差異是標籤。 所以,如果你讀它,那麼你就大功告成了

,如果你喜歡,你可以使用Selenium做

第1步:找到的XPath - 獲取按鈕的XPath:爲正確打開網頁在其Chrome瀏覽器點擊選擇檢查元素 - 這將打開突出顯示的行,並選擇複製XPath 的HTML文件,然後右鍵單擊 - 中的XPath複製在記事本

現在,你有XPath的,你可以選擇按鈕通過Python腳本查詢屬性

這裏是一個原型

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox() 
driver.get("http://www.youradress.org")#put here the adress of your page 
elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath 
print(elem .get_attribute("class")) 
driver.close() 

希望幫助,如果您有疑問,請讓我知道

我使用這些鏈接,文檔

Python Selenium: Find object attributes using xpath

http://selenium-python.readthedocs.org/en/latest/locating-elements.html

+0

我嘗試過使用硒,但因爲它是我登錄的頁面,所以似乎沒有工作。 – rjbogz 2015-01-09 21:59:45

+0

你觀察到了什麼?你也可以使用硒進行登錄。你試過了嗎? – Gabriel 2015-01-09 22:28:19

+0

我正在使用'toast_link = driver.find_element_by_link_text('Toast')',但現在我需要通過它們光標。我可以在驅動程序中使用類似於for find_element_by_link_text('Toast')的for循環:'? – rjbogz 2015-01-10 16:29:20