Get multiple elements from span inside of div - Python 3 Selenium - html

I am trying to extract two elements from span inside of div.
The HTML code is:
<div id="market_commodity_buyrequests">
<span class="market_commodity_orders_header_promote">16337</span>
" requests to buy at "
<span class="market_commodity_orders_header_promote">$6.67</span>
" or lower"
</div>
I am trying to extract the second element (the price) from span but can only seem to get the first element to show up. What am I doing wrong?
Here is my code:
driver.get('https://steamcommunity.com/market/listings/730/AK-47%20%7C%20Redline%20%28Field-Tested%29')
data = driver.find_element_by_xpath('//div[#id="market_commodity_buyrequests"]//span[#class="market_commodity_orders_header_promote"]')
print(data.text)
which returns the response:
16337
I need it to return the "$6.67" instead. Thanks!

You are using find_element which is singular and only gets one element.
Use find_elements to get all of them and then loop through them to find your specific need.

Related

How to find the index of HTML child tag in Selenium WebDriver?

I am trying to find a way to return the index of a HTML child tag based on its xpath.
For instance, on the right rail of a page, I have three elements:
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[1]/h4
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[2]/h4
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[3]/h4
Assume that I've found the first element, and I want to return the number inside the tag div, which is 1. How can I do it?
I referred to this previous post (How to count HTML child tag in Selenium WebDriver using Java) but still cannot figure it out.
You can get the number using regex:
var regExp = /div\[([^)]+)\]/;
var matches = regExp.exec("//[#id=\"ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880\"]/div[2]/h4");
console.log(matches[1]); \\ returns 2
You can select preceeding sibling in xpath to get all the reports before your current one like this:
//h4[contains(text(),'hello1')]/preceding-sibling::h4
Now you only have to count how many you found plus the current and you have your index.
Another option would be to select all the reports at once and loop over them checking for their content. They always come in the same order they are in the dom.
for java it could look like this:
List<WebElement> reports = driver.findElements(By.xpath("//*[#id='ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880']/div/h4")
for(WebElement element : reports){
if(element.getText().contains("report1"){
return reports.indexOf(element) + 1;
}
}
Otherwise you will have to parse the xpath by yourself to extract the value (see LG3527118's answer for this).

How to get correct class element from HTML using VBA

I am trying to extract this website using VBA
This is the HTML I want to Target
<div class="claim"> <div num="1" id="US-6627754-B2-CLM-00001" class="claim">
to get data for various numbers, only constant things is
<div class ="claims" and class ="claim"
rest of the html is dynamic.
following code just works fine for first element.
oHtml.getElementsByClassName("claim").Item.innerHTML
for this case
oHtml.getElementsByTagName("div")(90).innerHTML
also gives desired result.
I don't have basic knowledge of HTML or VBA.
I know that this is not correct as it gives duplicate entry one for div class="claim" and other for class="claim" and div(90) is not always constant.
How to just target
<div class = "claim"
Tried oHtml.getElementsByTagName("div")(90).getElementsByClassName("claim").Item.innerHTML
but did not gave results. For this specific website it is at div 90. again its not constant.
Have you tried finding ir by using the id as this is uniqe and can only be used once so you won't get duplicates ! On my phone atm so can't give eg if you need I will when on my computer! I'm studying both html and vba not a pro but I think I know what i'm doing!

Selenium: How to get text from within an html tag which has another tag in it

<a class="spf-link current" href="/orders/returns?offset=0&limit=25">
<span class="pg-helpText">Page</span>
1
</a>
I need to read the value '1' in my test. When I do a get text using css-selector .spf-link.current, it gets me "Page 1". I only need '1'. How do I exclude the text from the span tag.
You will need Javascript childNodes to get the text from the nodes.
The childNodes property returns a collection of a node's child nodes,
as a NodeList object.
The nodes in the collection are sorted as they appear in the source
code and can be accessed by index numbers. The index starts at 0.
WebElement element = driver.findElement(By
.cssSelector(".spf-link.current"));
String node_text=(String)((JavascriptExecutor)driver)
.executeScript("return arguments[0].childNodes[2].nodeValue",element);
This should give you the value 1 as node_text when you run the JavascriptExecutor. Other way is to split the text Page 1 on the basis of space. But, i would prefer childNodes.
Let me know if you have any queries.
Try the following:
//span[#class="pg-helpText" and contains(text(), 'Page')]/following-sibling::span/span
It is not normal to customize untagged text between tags.
I recommend you to change the value to your desired result:
<a class="spf-link current" href="/orders/returns?offset=0&limit=25">1</a>
...and when you want to show it in other place, do it as var ThePage = "Page " + [the page number]
Other solution is to include it into another tag as a div or as another span with a different class or id.

can there be a divs method in PageObject::Accessors and is my workaround ok?

In page object I would like to have access to multiple divs in a particular way.
This is access to the first div that matches:
div(:search_result, id: /rptSearchResults/)
This would be access to multiple divs that match if divs existed in PageObject::Accessors:
divs(:search_result, id: /rptSearchResults/)
So far I have tried:
visit_page SearchResultsPage do |page|
#This outputs the first div that matches
page.search_result_element.div_elements(id: /rptSearchResults/).each { |i| puts i.text }
#This accesses the page and outputs text in all divs that match
page.div_elements(id: /rptSearchResults/).each { |i| puts i.text}
end
Can anyone suggest a better way of doing this within page-object?
Thanks in advance...
By using the way you defined multiple divs, divs(:search_result, id: /rptSearchResults/), you can access the elements by search_result_elements.
Some examples:
Get the first div element - search_result_elements[0]
Get the length of elements returned - search_result_elements.length
Get the texts of the divs - search_result_elements.map(&:text)
Get a child element from the third div - search_result_elements[2].element
Hope this helps!

<span> </span> in Table<td>

I have a time table as below:
<table id="timetable">
<tr><td>00:00</td><td id="tm0"></td></tr>
<tr><td>00:30</td><td id="tm1"></td></tr>
<tr><td>01:00</td><td id="tm2"></td></tr>
<tr><td>01:30</td><td id="tm3"></td></tr>
</table>
so if i want insert <span></span> into <td> start from id="tm0" - id="tm3" how can i make it ?? instead of "rowspan" can anyone help ??
First get td elements using getElementById if you want frm 0 to 3 loop through the elements using something like ById("tm"+i).
Create the span element using creatingElement and set its properties.
Append this new element to the element retrieved in setp 1.
http://www.w3schools.com/DOM/dom_element.asp
But you are asking for this, in place of rowspan- which carries dynamic values. So i'm not user if i answered your query. But let us know why you want to use span in place of rowspan ?
Using jquery you can do it like following process:
$('table td:last-child').append('<span></span>');
You can't. Elements are arranged in a hierarchy. If the start tag for X is a child of Y, then the end tag for X must also be a child of Y.