How to use Watir to find a specific link when all the links have the same display text? - watir

The links are wrapped in a span:
<span class='editbio'>
Edit
</span>
...
<span class='addbio'>
Edit
</span>

How about something like
browser.span(:class, "editbio").link(:text, "Edit")
to get the first link
vs
browser.span(:class, "addbio").link(:text, "Edit")
for the second link?
You can get a list of elements and tags you can use at Watir: Methods supported by Element.

You can try to use multiple arguments with the Watir's link method:
http://wiki.openqa.org/display/WTR/Multiple+Attributes
For your example, the accepted answer works fine. But if both of your links were in a single span and had the same display text, like so:
<span class='edit'>
Edit
Edit
</span>
You could use something like this for the second link (zero-based index):
browser.span(:class, 'edit').link(text: 'Edit', index: 1)

Related

How can I get text from span(s) in dirs which are located in table?

I have just only recently started using selenium and I can't get it to work properly.
I need to get a text from (s) which are inside dives which are inside a table.
Like this:
<td class="some-class">
"""many divs above"""
<div class="props">
<span>text</span>
<span class="mr2">text</span>
</div>
"""/many divs above"""
</td>
So, I need to get 'text(s)' from spans, but also there are many td s like this one and inside divs, classes repeat.
Moreover, how can I get it as a list?
I have tried this:
el = browser.find_elements_by_css_selector('dir.props > span')
print(el)
for i in el:
dict_of_orders.append(i.text)
print(dict_of_orders)
However, it returns only empty lists.
Any advice?:)
Thanks ahead for any help!
You have a typo in your CSS selector. It should be div instead of dir:
el = browser.find_elements_by_css_selector('div.props > span')
I don't know if that will fix your problem, but you'll have to fix that.
Another thing I would try is, instead of text, use the innerText attribute, like this:
dict_of_orders.append(i.get_attribute('innerText'))
Hello for future someone like me. My problem was solved by adding the delay.
Check it, maybe a site that you're trying to parse is slow.
time.sleep(5) was the answer.
el =browser.find_elements_by_xpath("//div[#class='props']/span")

How to find and click the link using “onclick” with Selenium/Python which includes quotes or double quotes?

How to find and click the link where “onclick” attribute value includes quotes? An Example below:-
<li onclick="StoreSessionifo('09:30 PM','84609','0023','SCREEN 1','2019-08-18T21:30:00','HO00005319');">
<a>09:30 PM</a>
<span class="info-mexperience">2D</span>
</li>
You can use xpath, utilizing the contains function, follow the following functions :
//tagname[contains(#attribute,"partialcontents")]
In your case, please try :
driver.find_element(By.XPATH, '//li[contains(#onclick,"84609")]').click()

Clicking first link/ patent after search in Google Patents using Selenium in Python

I am trying to click on the first result from the Google patents search page using selenium in python with chromedriver.
It automatically types the company from a list in the search bar and enters.
driver.get("https://patents.google.com/")
searchbar = driver.find_element_by_id("searchInput")
searchbar.send_keys(Company[i], Keys.ENTER)
This is what I used after in order to find the first link and execute the click.
time.sleep(20)
element = driver.find_element_by_xpath("//a[#id='link']")
element[0].click()
However, it does not identify the element for some reason and won't click the link.
This is the HTML:
<article class="result style-scope search-result-item">
<state-modifier class="result-title style-scope search-result-item" act="{"type": "OPEN_RESULT", "result": "$result"}" data-result="patent/US3334718A/en"><a id="link" href="/patent/US3334718A/en?assignee=AAR+CORP&oq=AAR+CORP" class="style-scope state-modifier">
<h3 class="style-scope search-result-item"><raw-html class="style-scope search-result-item">
<span id="htmlContent" class="style-scope raw-html" style="display: inline;"> Cargo handling appartus</span>
I hope it makes sense. Thanks!
It looks that you've forgot to invoke click method.
Try to alter your code a little
time.sleep(20)
element = driver.find_element_by_xpath("//a[#id='link']")
element[0].click() # <- add parentheses to invoke the method
This is caused, since the functions are objects just like any other object in Python.
If you forget to add parentheses, you are referring to the function object, not the function itself.

How to use URL output filter with multi-select TV?

I have multi-select TV, and I have set URL output filter. I receive code like
<a title="title1" href="title1">Индия</a>
<br>
<a title="title2" href="title2">Тибет</a>
<br>
<a title="title2" href="title3">Мустанг</a>
Everything is OK, but I don't need the br tag, how to discard it?
First way to do it - create your own output type based on url output type (core/model/modx/processors/element/tv/renders/web/output/url.class.php).
Second way - use output modifier "replace":
[[*my_urls_tv:replace=`<br>==`]]

How to click on link element with specific text using watir?

I'm not able to click on text link 'Add' using watir:
PAGE:
<div id="divAdd" style="float: right">
<a onclick="SwitchView('2')" style="color: #1B56A7; cursor: pointer;">Add</a>
</div>
Watir CODE:
browser.link(:text =>"Add").click
EXCEPTION:
Unable to locate element, using {:tag_name=>["a"], :text=>"Add"}
Please help me how to handle this?
If the page has a lot of ajax and javascript going on, you may just have to wait a little bit for the client side code to finish rendering the page after it has been loaded from the browser.
Try this
browser.link(:text =>"Add").when_present.click
If that does not work, then make sure the item is not in a frame or something..
btw, if there is more than one link on the page with the text 'Add' then you may have to specify a container outside the link that lets you identify which link you want. eg.
browser.div(id: => "divAdd").link.when_present.click
If
This would be my way of doing it.
while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do
browser.div(:class, 'containerDIV').a(:text, 'Add').click
end

Resources