dandepeched dandepeched - 2 months ago 13
HTML Question

Selenium returns wrong element when search by class/xpath/css

I'm trying to make simple python script that finds non-faded elements on site and click them.

My code:

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()


In this script Selenium returns wrong element with the similar class name - "giveaway__row-inner-wrap is-faded".

I've tried xpath and css selectors, but result was the same.
It looks like Selenium searches not the exact class name, but the one which contains search query.

Another strange thing is that the same code was working fine nearly 2 days ago. But nothing was changed or updated since that time.

Here is example of both types of HTML elements:

<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>


I would be appreciate if someone can help me with this!

Answer

What you're seeing is correct. The element

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

has in fact two classes: giveaway__row-inner-wrap and is-faded therefore it's correct that Selenium returns it. I suspect that you have two elements in that list, you just need to iterate it and locate the one element that does not also have the is-faded class.