Home > OS >  Selenium finding elements returns incorrect elements
Selenium finding elements returns incorrect elements

Time:10-09

I'm using Selenium to try and get some elements on a web page but I'm having trouble getting the ones I want. I'm getting some, but they're not the ones I want.

So what I have on my page are five divs that look like this:

<div class="membershipDetails">

Inside each one is something like this:

<div class="membershipDetail">
<h3>
    <a href="/EditMembership/111112">VIP Membership</a>
</h3>    
</div>

They DO all have this same link, but they don't have the same text ('VIP Membership' would be replaced by something else)

So the first thing was to get all the divs above in a list. This is the line I use:

listElementsMembership = driver.find_elements_by_css_selector(div[class^='membershipDetail'])

This gives me five elements, just as I would expect. I checked the 'class' attribute name and they are what I would expect. At this point I should say that they aren't all EXACTLY the same name 'membershipDetail'. Some have variations. But I can see that I have all five.

The next thing is to go through these elements and try and get that element which contains the href ('VIP Membership').

So I did that like this:

for elem in listElementsMembership:
elemDetailsLink = elem.find_element_by_xpath('//a[contains(@href,"EditMembership")]')

Now this does return something, but it always got me the element from the FIRST of the five elements. It's as if the 'elem.find_element_by_xpath' line is going up a level first before finding these hrefs. I kind of confirmed this by switching this to a 'find_elements_by_xpath' (plural) and getting, you guessed it, five elements.

So is this line:

elemDetailsLink = elem.find_element_by_xpath('//a[contains(@href,"EditMembership")]')

going up a level before getting its results? If it is, now can I make it not do that and just restrict itself to the children?

CodePudding user response:

You've few mistake in that css selector.

  1. Quotes are missing.
  2. ^ is for starts-with, not sure if you really need that. In case it's partial matching please use * instead of ^

Also, I do not see any logic for the below statement in your code attempt.

The next thing is to go through these elements and try and get that element which contains the href ('VIP Membership').

Code :

listElementsMembership = driver.find_elements_by_css_selector("div[class*='membershipDetail']")

for ele in listElementsMembership:
    e = ele.find_element(By.XPATH, ".//descendant::a")
    if "VIP Membership" in e.get_attribute('href'):
        print(e.text, e.get_attribute('href'))

CodePudding user response:

If you are trying to find element with in an element use a . in the xpath like below:

listElementsMembership = driver.find_elements_by_css_selector(div[class^='membershipDetail'])

for elem in listElementsMembership:
    elemDetailsLink = elem.find_element_by_xpath('.//a') # Finds the "a" tag with respect to "elem"

Suppose if you are looking for VIP Membership:

listElementsMembership = driver.find_elements_by_css_selector(div[class^='membershipDetail'])

for elem in listElementsMembership:
    value = elem.find_element_by_xpath('.//a').get_attribute("innerText")
    if "VIP Membership" in value:
        print(elem.find_element_by_xpath('.//a').get_attribute("innerText"))

And if you dont want iterate over all the five elements try to use xpath like below: (As per the HTML you have shared)

//div[@class='membershipDetail']//a[text()='VIP Membership']
Or
//div[@class='membershipDetail']//a[contains(text(),'VIP Membership')]

CodePudding user response:

You can give an index using a square bracket like this.

elemDetailsLink = elem.find_element_by_xpath('(//a[contains(@href,"EditMembership")])[1]')

If you are trying to get an element using XPath, the index should start with 1, not 0.

  • Related