2017-04-02 23 views
0

我試圖從圖片類profile_view_img_5087188獲取所有圖片,但在頁面源代碼中有多個類「profile_view_img_xxxxxx」,並且它們包含的圖片的相同網址的數量較少包括圖像按類別獲取圖片並使用Selenium Python進行點擊

http://i.imgur.com/oraB49H.png 

類,我想如果點擊上面的圖片網址找到

<head><style type="text/css">.profile_view_img_5087188 { background-image: url('http://i.imgur.com/oraB49H.png'); }</style></head> 



<a class="cursor earn_pages_button profile_view_img_5087188" onclick="startlink52efa('newlookskincenter','follow','99218',16837);imageWin('http://instagram.com/newlookskincenter','Instagram','99218','newlookskincenter','qFVnV5xlY2OJaW4','1015','500','follow',16837)"></a> 

這裏是我的代碼,但不工作

  like_button = browser.find_elements_by_css_selector("div > span > a") 
      for links in like_button: 
        if "http://i.imgur.com/oraB49H.png" in link.get_attribute('href'): 
          links.click() 
+0

我沒有看到在上面提供任何標籤'href'屬性,你可以添加一些更多的HTML,這有助於澄清更多 – NarendraR

+0

圖像源是在樣式表 Bostan

回答

0

首先,您需要獲得所有<a>標籤,其中的類名稱profile_view_img_5087188的編號是不同的。因此,使用下面的XPath得到所有標籤:

like_button = browser.find_elements_by_xpath("//a[contains(@class,'profile_view_img')]") 

然後遍歷所有鏈接,並找到你的圖片網址

for links in like_button: 
if "http://instagram.com/newlookskincenter" in link.get_attribute('onclick'): 
    links.click() 

注:請確保您使用的是正確的字符串比較,因爲我不多知道與Python語法

+0

@Bostan,試試這個答案,讓我知道如果有任何問題 – NarendraR

+0

不工作,請幫助.. – Bostan

+0

是你得到任何錯誤? – NarendraR