How I can access elements via a non-standard html property? - watir

I'm try to implement test automation with watir-webdriver. By the way I am a freshman with watir-webdriver, ruby and co.
All our HTML-entities have a unique HTML-property named "wicketpath". It is possible to access the element with "name", "id" a.s.o, but not with the property "wicketpath". So I tried it with XPATH but I have no success.
Can anybody help me with a codesnippet how I can access the element via the propertie "wicketpath"?
Thanks in advance.
R.

You should be able to use xpath.
For example, consider the following HTML
<ul class="ui-autocomplete" role="listbox">
<li class="ui-menu-item" role="menuitem" wicketpath="false">Value 1</li>
<li class="ui-menu-item" role="menuitem" wicketpath="false">Value 2</li>
<li class="ui-menu-item" role="menuitem" wicketpath="true">Value 3</li>
</ul>
The following xpath will give the text of the li that has wicketpath = true:
puts browser.li(:xpath, "//li[#wicketpath='true']").text
#=>Value 3
Update - Alternative solution - Adding To Locators:
If you use a lot of wicketpath, you could add it to the locators.
After you require watir-webdriver, add this:
# This allows using :wicketpath in locators
Watir::HTMLElement.attributes << :wicketpath
# This allows accessing the wicketpath attribute
class Watir::Element
attribute(String, :wicketpath, 'wicketpath')
end
This will let you use 'wicketpath' as a locator:
p browser.li(:wicketpath, 'true').text
#=> "Value 3"
p browser.li(:text, 'Value 3').wicketpath
#=> true

Try this
puts browser.li(:css, ".ui-autocomplete > .ui-menu-item[wicketpath='true']").text
Please Let me know is the above scripting is working or not.

Related

How can i click the third href link?

<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[#id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag.
The code keeps clicking on the second tag.
Try
driver.find_element_by_xpath("//ul[#id='pairSublinksLevel2']/li[3]/a").click()
EDIT:
Thanks #DebanjanB for suggestion:
When you get the element with xpath //ul[#id='pairSublinksLevel2'] and search for a tag in its child elements, then it will return the first match(In your case, it could be inside second li tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
As per the HTML you have shared you can use either of the following solutions:
Using link_text:
driver.find_element_by_link_text("last data").click()
Using partial_link_text:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath:
driver.find_element_by_xpath("//ul[#class='arial_12 newBigTabs' and #id='pairSublinksLevel2']//a[contains(#href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver

how to click on the first result of a search on metacritc with selenium

how do I click on the first search result on metacritc's search bar?
this is what I have so far:
self.search_element = self.driver.find_element_by_name("search_term")
self.search_element.clear()
self.search_element.send_keys(self.game_line_edit.text())
self.link_to_click = self.driver.find_element_by_name("search_results_item")
self.link_to_click.click()
# self.game.setText(self.driver.find_element("search_results_item"))
self.game_line_edit.setText("")
self.driver.close()
but I'm getting this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"search_results_item"}
I realize selenium can't find the link but I am not sure what element it is
this is the HTML I'm trying to click on:
<a class="search_results_item" href="https://www.metacritic.com/game/pc/into-the-breach">
<span class="metascore_w score_outstanding">90</span>
<span class="title" data-mctitle="Into the Breach"><b>Into the</b> Breach</span>
<span class="type secondary">PC Game</span>
<span class="separ secondary">,</span>
<span class="date secondary">2018</span>
</a>
can someone help?
Thanks!
You are searching by name when you are referring to a class. Instead use a CSS selector, e.g.
.find_element_by_css_selector(".search_results_item")
.search_results_item indicates a class with the name 'search_results_item'.
If that doesn't work, you probably need a wait. See this answer for more info.
If you're fine with using xPath, you can select by index.
(//a[contains(#class,'search_results_item')])[1]
#JeffC is also correct as well. You should not select by name because this element has no name. At the very least, select by class or tag.

How do you add an attribute to your CSS selector to specify specific pagination link?

I just got into Scrapy & I’m aware this is a Noob question but How do I add an attribute to specify specific pagination link?
here is the html with the element I’m targeting.
`<div class="pagination">
<a rel="prev" href="/collections/all?page=1" class="fa fa-chevron-left prev pagination-icon"></a>
<ul>
<li class="pagination-icon">
1
</li>
<li class="pagination-icon pagination-icon--current">
2
</li>
<li class="pagination-icon">
3
</li>
<li class="pagination-icon">
4
</li>
<li class="pagination-icon pagination-icon--current">
…
</li>
<li class="pagination-icon">
50
</li>
</ul>
I Need to follow the link in this line
<a rel="next" href="/collections/all?page=3" class="fa fa-chevron-right next pagination-icon"></a>
Here is my scrapy code
next_page = response.css('div.pagination a::attr(href)').extract_first()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
What’s happening is its following this link instead of the other one because it is the first one in the class “pagination”
<a rel="prev" href="/collections/all?page=1" class="fa fa-chevron-left prev pagination-icon"></a>
I can see 2 differences between the attributes of the 2 links, both in the class “pagination”
Rel attribute is different, I need the one with “next”
Class attribute is different, I need “fa fa-chevron-right next pagination-icon”
I’m pretty sure I can get the correct link by specifying one of the 2 attributes listed above in my css selector. I tried using the following CSS selectors but none worked.
div.pagination a.fa fa-chevron-right next pagination-icon a::attr(href) does not work
a.fa fa-chevron-right next pagination-icon a::attr(href) does not work
a.fa fa-chevron-right next pagination-icon::attr(href) does not work
How can I achieve my goal? Why do none of the CSS selectors I tried work?
You can't select multiple classes with a single dot. Either combine each of them with dots or go for this syntax "[class='fa fa-chevron-right next pagination-icon']". However, if any class out of them is generated dynamically then the selector will break.
Then try with this to see what happens.
response.css('div.pagination a[rel="next"]::attr(href)').extract_first()

getelementsbyID inner dt id values

I am extracting data from HTML using Vb Script. This is the HTML code from which am trying to extract the data.
<dl id="overview">
<dt id="overview-summary-current-title" class="summary-current" style="display:block">
Current
</dt>
<dd class="summary-current" style="display:block">
<ul class="current">
<li>
Software Engineer
<span class="at">at </span>
<a class="company-profile-public" href="/company/ABC Systems?trk=ppro_cprof">
<span class="org summary">ABC Systems</span></a>
</li>
</ul>
</dd>
In my previous question, I had asked for a similar doubt. The link is Excel getElementById extract the span class information.
However, in that case, I wanted to extract the information corresponding to the dl id and it also had span id. In this case, I need to extract the information corresponding to the dt id.
In my VB Script, I tried something like this.
Dim openedpage as String
openedpage = iedoc1.getElementById("overview").getElementById("overview-summary-current-title").innerHTML
However, I am getting no output.
I want the output as Software Engineer at ABC systems.
Kindly help me out.
The object returned by getElementById() doesn't have a method .getElementById(), so the following line fails:
.getElementById("overview").getElementById("overview-summary-current-title")
If you don't get any output, not even an error message, you probably have On Error Resume Next somewhere in your script. Please don't use that unless you know exactly what you're doing and have sensible error handling code in place.
Also, the element with the ID "overview-summary-current-title" is this:
<dt id="overview-summary-current-title" class="summary-current" style="display:block">
Current
</dt>
So you couldn't possibly extract the text "Software Engineer at ABC systems" from that element.
Try selecting the first <ul> tag from the element with the ID "overview", and then use the innerText property instead of the innerHtml property:
Set ie = CreateObject("InternetExplorer Application")
ie.Navigate "..."
While ie.Busy : WScript.Sleep 100 : Wend
Set e1 = ie.document.getElementById("overview")
Set e2 = e1.getElementsByTagName("ul")(0)
WScript.Echo e2.innerText

Nested GPath expressions with XmlSlurper and findAll

I'm trying to analyse an XML tree using XmlSlurper and GPath, and the behaviour of the findAll method confuses me.
Say, for example, that you have the following XML tree:
<html>
<body>
<ul>
<li class="odd"><span>Element 1</span></li>
<li class="even"><span>Element 2</span></li>
<li class="odd"><span>Element 3</span></li>
<li class="even"><span>Element 4</span></li>
<li class="odd"><span>Element 5</span></li>
</ul>
</body>
</html>
Assuming that xml has been initialised through one of XmlSlurper's parse methods, the following code executes as one would expect:
// Prints:
// odd
// odd
// odd
xml.body.ul.li.findAll {it.#class == 'odd'}.#class.each {println it.text()}
On the other hand:
// Doesn't print anything.
xml.body.ul.li.findAll {it.#class == 'odd'}.span.each {println it.text()}
I'm struggling to understand why I can use the special # property (as well as others, such as **), but not 'normal' ones.
I've looked at the API code, and what confuses me even more is that the getProperty implementation (found in GPathResult) seems to support what I'm trying to do.
What am I missing?
You need to iterate over every span, so you can use the spread-dot operator:
xml.body.ul.li.findAll {it.#class == 'odd'}*.span.each {println it.text()}

Resources