xpath preceding-sibling solution - python

Now I need to get content of class odd or just text from <td> 161.5 </td>
so i wrote:
element = driver.find_elements_by_xpath('//td[span[#class=" odds-wrap " and #eu="1.90"]]/preceding-sibling::td')
and and it works.
My question is: Is it possible to get the same content using one more condition, for example title="bet365".. So, I want to get the same result, but using one more condition from another sibling element..
edit
element = driver.find_elements_by_xpath('//tr[preceding-sibling::td/span[#class=" odds-wrap " and #eu="1.90"] and following-sibling::td/div/a[#title="bet365"]]')
for ele in element:
print(ele.text)
not find and print anything, I do not know why

You can combine preceding-sibling and following-sibling:
//td[following-sibling::td/span[#class=" odds-wrap " and #eu="1.90"] and preceding-sibling::td/a[#title="bet365"]]

Use the following xpath
//span[#class=" odds-wrap " and #eu="1.90"]/preceding::a[#title='bet365']/following::td[1]

Related

Selenium Python - how to get deeply nested element

I am exploring Selenium Python and trying to grab a name property from Linkedin page in order to get its index later.
This is the HTML:
Here is how I try to do it:
all_span = driver.find_elements(By.TAG_NAME, "span")
all_span = [s for s in all_span if s.get_attribute("aria-hidden") == "true"]
counter = 1
for i in all_span:
print(counter)
print(i.text)
counter += 1
The problem is there are other spans on the same page that also have aria-hidden=true attribute, but not relevant and that messes up the index.
So I need to reach that span that contains name from one of its its parent divs but I don't know how.
Looking at documentation here: https://selenium-python.readthedocs.io/locating-elements.html# I cant seem to find how to target deeply neseted elements.
I need to get the name that is in span element.
The link
The best way would be to use xpath. https://selenium-python.readthedocs.io/locating-elements.html#locating-by-xpath
Let's say you have this:
<div id="this-div-contains-the-span-i-want">
<span aria-hidden="true">
<!--
...
//-->
</span>
</div>
Then, using xpath:
xpath = "//div[#id='this-div-contains-the-span-i-want']/span[#aria-hidden='true']"
span_i_want = driver.find_element(By.XPATH, xpath)
So, in your example, you could use:
xpath = "//a[#class='app-aware-link']/span[#dir='ltr']/span[#aria-hidden='true']"
span_i_want = driver.find_element(By.XPATH, xpath)
print(span_i_want.text)
No typos but
print(span_i_want) - returns [] empty array

How to search for an element in the same class as another (Selenium Python)

If I have already searched for an element, is there any way I can find another in the same class?
ex.
<div class="day">
<span class="day_number">4</span>
<span class="day_item_time" data-day-total-time="day-total-time">10m</span></div>
So I had to search by the date (finding the element that had the 4), but how do I translate that to finding the 10m?
This was the result after printing out the element <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="4ff20f1a-75d1-42c1-a7d0-de0c532651a6", element="1147f2d5-8832-44d4-a906-929ebaaa49e2")>
Try This below code.It should return your expected output.
driver.find_element_by_xpath("(//div[#class='day']/span)[1]").text
driver.find_element_by_xpath("(//div[#class='day']/span)[2]").text
Output:
4
10m
Something like this maybe:
//span[text()="4"]/following-sibling::span
You can get day-total-time using xpaths below.
Get span with day_number class and text 4, ancestor - get first parent div with class day and then get span with data-day-total-time attribute:
//span[#class='day_number' and .='4']/ancestor::div[#class='day'][1]/span[#data-day-total-time]
Get div with class day and child span with day_number class and text 4 and and then get span with data-day-total-time attribute:
//div[#class='day' and ./span[#class='day_number' and .='4']]/span[#data-day-total-time]

Xpath for specific child element of a parent element which is not distinguished by class?

The html snippet is like this:
<div class="busi-attr">
<p><span class="attrName">Min. Order: </span>1 Piece</p>
<p><span class="attrName">Supply Ability: </span>10,000 Piece/Pieces per Month</p>
</div>
I only want the second <p> element that is amount 10,000 and nothing else, how do i do that? thanks
Try xpath with a list comprehension that checks the text value of an element:
span=[x for x in yourwebelement.xpath('//span[#class="attrName"]') if 'supply ability' in x.text.lower()][0]
This is only the span of course, but all you need now is the parent
p=span.xpath('.//parent::p')[0]
I think the span block stops you from getting the text value of p, so let's get all the text minus whatever is in the span.
text=[x for x in p.itertext() if x != span.text]

Select button by highest number in xpath

There are multiple buttons on my page containing similar href. They only differ with id_invoices. I want to click one button on page using xpath and href which looks like:
href="/pl/payment/getinvoice/?id_invoices=461"
I can select all buttons using:
invoices = driver.find_elements_by_xpath("//a[contains(#href, '/payment/getinvoice/')]")
but I need to select only button with highest id_invoices. Can it be done? :)
What you can do is:
hrefList = driver.find_elements_by_xpath("//a[contains(#href, '/payment/getinvoice/')]/#href")
for i in hrefList:
hrefList[i]=hrefList[i].split("id_invoices=")[-1]
max = max(hrefList)
driver.find_elements_by_xpath("//a[contains(#href, '/payment/getinvoice/?id_invoices="+str(max))+"'"+"]").click()
I don't know much about python so giving you a direction/algorithm to achieve same
Using getAttribute('#href');
You will get strings of URLs
You need to split all element after getText() you get in invoice List.
Split by= and pick up the last array value.
Now you need to typecast string to int, as last value after = will be a number
Now you just need to pick the highest value.
Since you have an XPath that returns all the desired elements, you just need to grab the href attribute from each one, split the href by '=' to get the id (2nd part of string), find the largest id, and then use the id to find the element you want and click on it.
invoices = driver.find_elements_by_xpath("//a[contains(#href, '/payment/getinvoice/')]")
ids = []
for invoice in invoices
ids.append(invoice.get_attribute("href").split('=')[2])
results = list(map(int, ids)) // you can't do max on a list of string, you won't get the right answer
id = max(results)
driver.find_element_by_xpath("//a[#href='/pl/payment/getinvoice/?id_invoices=" + id + "']").click

Selenium Python how to get text(html source) from <div>

I'm trying to get text $27.5 inside tag <div>, I located the element by id and the element is called "price".
The snippet of html is as follows:
<div id="PPP,BOSSST,NYCPAS,2015-04-26T01:00:00-04:00,2015-04-26T05:20:00-04:00,_price" class="price inlineBlock strong mediumText">$27.50</div>
Here is what I've tried
price.text
price.get_attribute('value')
Both of the above doesn't work.
Update:
Thanks for everyone that tries to help.
I combined your answers together and got the solution:)
price = driver.find_element_by_xpath("//div[#class='price inlineBlock strong mediumText']")
price_content = price.get_attribute('innerHTML')
print price_content.strip()
Can't you use a regular expression or Beautiful Soup to find the contents of the element in HTML:
re.search(r'<div.*?>(*.?)</div>', price.get_attribute('innerHTML')).group(1)
You element is hidden, last I worked with Selenium you were not able to get text of hidden elements. That said, you can always execute javascript, I dont usually write in python, but it should be something like:
def val = driver.execute_script("return document.getElementById('locator').innerHTML")
Change the css selector to
div[id$='_price']
Complete code
price = fltright.find_element(By.CSS_SELECTOR, "div[id$='_price']")
price.text
I tried your edited solution, but they only get 1 div having class. So, I tried these below to print a List of div having the same class.
Changing element to elements will output a List:
price = driver.find_elements_by_xpath('//div[#class = "price inlineBlock strong mediumText"]')
Use for ... in range () to print a List:
num = len (price)
for i in range (num):
print (price[i].text)
browser.find_element_by_xpath("//form[#id='workQueueTaskListForm']/div[1]/p").text

Categories