2016-09-17 65 views
0

我試圖製作一個簡單的python腳本,在網站上查找未褪色的元素並單擊它們。Selenium按class/xpath/css搜索時返回錯誤的元素

我的代碼:

from selenium import webdriver 
ffprofile = webdriver.FirefoxProfile(r"C:/Users/Dan/AppData/Roaming/Mozilla/Firefox/Profiles/q3fhuchn.QAtest") 
driver = webdriver.Firefox(ffprofile) 

driver.get("https://www.steamgifts.com/giveaways/search?page=1&type=wishlist") 
element = driver.find_element_by_class_name("giveaway__row-inner-wrap") 
element.find_element_by_class_name("giveaway__heading__name").click() 

在這個腳本硒返回錯誤的元素與同級的名字 - 「giveaway__row,內包裝是褪」。

我試過xpath和css選擇器,但結果是一樣的。 它看起來像Selenium不是搜索確切的類名,而是包含搜索查詢的類。

而且腳本正常工作與下面的表達式:

driver.find_element_by_xpath("//div[@class='giveaway__row-in‌​ner-wrap']//a[@class‌​='giveaway__heading_‌​_name']").click() 

但我需要的所有元素的列表,以便能夠重複它們。

這裏是例如兩種類型的HTML元素組成:

<div class="giveaway__row-outer-wrap" data-game-id="707524220"> 
     <div class="giveaway__row-inner-wrap is-faded"> 
      <div class="giveaway__summary"> 
       <h2 class="giveaway__heading"> 
        <a class="giveaway__heading__name" href="/giveaway/FFdQd/call-of-duty-black-ops-iii-multiplayer-starter-pack">Call of Duty: Black Ops III - Multiplaye...</a><span class="giveaway__heading__thin">(15P)</span><a class="giveaway__icon" rel="nofollow" target="_blank" href="http://store.steampowered.com/app/437350/"><i class="fa fa-steam"></i></a><i data-popup="popup--hide-games" class="giveaway__icon giveaway__hide trigger-popup fa fa-eye-slash"></i> 
       </h2> 
       <div class="giveaway__columns"> 
        <div><i class="fa fa-clock-o"></i> <span title="Tomorrow, 4:59am">7 hours remaining</span></div><div class="giveaway__column--width-fill text-right"><span title="September 12, 2016, 4:01am">5 days ago</span> by <a class="giveaway__username" href="/user/Clockknight">Clockknight</a></div></div> 
        <div class="giveaway__links"> 
         <a href="/giveaway/FFdQd/call-of-duty-black-ops-iii-multiplayer-starter-pack/entries"><i class="fa fa-tag"></i> <span>2,552 entries</span></a> 
         <a href="/giveaway/FFdQd/call-of-duty-black-ops-iii-multiplayer-starter-pack/comments"><i class="fa fa-comment"></i> <span>25 comments</span></a> 
        </div> 
       </div><a href="/user/Clockknight" class="global__image-outer-wrap global__image-outer-wrap--avatar-small"><div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/51/5127d377a5bb8ee65356bcd81e44873824a2e7b5_medium.jpg);"></div></a><a class="global__image-outer-wrap global__image-outer-wrap--game-medium" href="/giveaway/FFdQd/call-of-duty-black-ops-iii-multiplayer-starter-pack"><div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steam/apps/437350/capsule_184x69.jpg);"></div> 
      </a> 
     </div> 
    </div> 
    <div class="giveaway__row-outer-wrap" data-game-id="8668"> 
     <div class="giveaway__row-inner-wrap"> 
      <div class="giveaway__summary"> 
       <h2 class="giveaway__heading"> 
        <a class="giveaway__heading__name" href="/giveaway/MsP6N/chaos-on-deponia">Chaos on Deponia</a><span class="giveaway__heading__thin">(100P)</span><a class="giveaway__icon" rel="nofollow" target="_blank" href="http://store.steampowered.com/app/220740/"><i class="fa fa-steam"></i></a><i data-popup="popup--hide-games" class="giveaway__icon giveaway__hide trigger-popup fa fa-eye-slash"></i> 
       </h2> 
       <div class="giveaway__columns"> 
        <div><i class="fa fa-clock-o"></i> <span title="Tomorrow, 7:00am">9 hours remaining</span></div><div class="giveaway__column--width-fill text-right"><span title="Today, 5:03pm">4 hours ago</span> by <a class="giveaway__username" href="/user/JsxfT">JsxfT</a></div></div> 
        <div class="giveaway__links"> 
         <a href="/giveaway/MsP6N/chaos-on-deponia/entries"><i class="fa fa-tag"></i> <span>84 entries</span></a> 
         <a href="/giveaway/MsP6N/chaos-on-deponia/comments"><i class="fa fa-comment"></i> <span>0 comments</span></a> 
        </div> 
       </div><a href="/user/JsxfT" class="global__image-outer-wrap global__image-outer-wrap--avatar-small"><div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/90/90b441d2627716a1a4c6be1f8fb375bea590c763_medium.jpg);"></div></a><a class="global__image-outer-wrap global__image-outer-wrap--game-medium" href="/giveaway/MsP6N/chaos-on-deponia"><div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steam/apps/220740/capsule_184x69.jpg);"></div> 
      </a> 
     </div> 
    </div> 

,我將不勝感激,如果有人可以幫助我!

回答

0

你看到什麼是正確的。元素

<div class="giveaway__row-inner-wrap is-faded"> 

實際上已經兩類:giveaway__row-inner-wrapis-faded因此它是正確的,硒返回。我懷疑你在這個列表中有兩個元素,你只需要迭代它,並找到一個而不是也有is-faded類的元素。

+0

謝謝!看起來像是這個問題。奇怪的是,它以前工作,但看起來像我需要重寫代碼,以排除'is-faded'類中的元素。 – dandepeched

+0

它可能以前工作過,因爲只有一個這樣的'div'或者因爲某種原因它被作爲列表中的第一個返回。 – CyberDude

+0

我發現以前的腳本使用下面的表達式 - ** driver.find_element_by_xpath(「// div [@ class ='giveaway__row-inner-wrap'] // a [@ class ='giveaway__heading__name']」)。click ()**現在它也正常工作。你知道爲什麼它只選擇非褪色元素嗎? – dandepeched

0

你可以嘗試點擊使用XPath所需的元素:

driver.find_element_by_xpath('//a[@class="giveaway__heading__name"]').click() 
0

我不知道爲什麼,但這個字符串完全適用於我的情況,所以我用它來創建非褪色元素的列表:

elements = driver.find_elements_by_xpath("//div[@class='giveaway__row-inner-wrap']//a[@class='giveaway__heading__name']") 
elements[x].click()