How to get fully qualified url with selenium on a link without any href attribute? - selenium

I would like to retrieve url from a link on an html page.
unfortunately, html code does not contain any href attribute (I suppose it is managed by some javascript code)
Here is html code :
<p class="ng-scope">
<a class="documentLink ng-binding" data-document-id="21928499">Electronic document</a>
</p>
I tried to do it with getattribute() function :
By linkPodPopover = new ByXpath("//div[#class='popover-content']//a[contains(.,'Electronic document')]");
find(linkPodPopover).getAttribute("href");
but it returns an empty String...
I also tried with this code but also without success :
driver.getCurrentUrl()
click(linkPodPopover)
Do you see another way ?

I did not find any answer on the internet.
And I tried to explore every javascript attribute of the DOM element of my link without finding URL.
Finally, I came across this problem by using browserstack functionnalities : http://browserstack.com/automate/java#enhancements-uploads-downloads
It allows to click on the download link, then the browser download it. then using Javascript, I can check if file is well downloaded, and if size and md5 are correct. –

Related

Why I can't find any selector with Xpath using two classes

I got a html page like this
<li class="class1 class2"></li>
but I try to find this selector by using
response.xpath('//li[#class="class1 class2"]')
I always got None.
I also try to run this code:
response.xpath('//li[#class="class1"]')
response.xpath('//li[#class="class1 class2"]')
Both return None
Who can tell me what's wrong?
Maybe I resolved but I don't understand. I found a phenomenon that I use DevTools on Chrome, that shows like this <li class="class1 class2"><\li>. But I use scrapy to crawl and return <li class="class1 class2 class3"><\li>
I encountered some phenomenon that the web page source code is different from the source code I crawled.
Main issue is your <li><\li> is not valid HTML like <li></li> and xpath should look like:
.//li[contains(#class, 'class1') and contains(#class, 'class2')]
May use a tool like xpather to check if a path works or not.

How store href from an image xpath selenium ide ui vision katalon recorder

I want to store href url from an image but when i try it show error
This is the code
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I want to store mysite.com
I already tried storeattribute with xpath and #href but show error (not available work for text link only)
Usually i use ui vision or katalon recorder but i never find a good solution at moment.
I need only xpath
Try this one out to get the parent a tag of the img src.
print(driver.find_element_by_xpath("img[src='//user/banners/16/08/1614708.gif']//parent::a").get_attribute('href'))

jquery featherlight on dynamic content from DataTalbes

I am using jquery featherlight (https://github.com/noelboss/featherlight) after trying to get jquery lightbox (https://lokeshdhakar.com/projects/lightbox2/) to work. I am using DataTables to generate a list. One of the items in the table is a link to a php page that returns links. The problem is I am calling an external PHP page that generates a list of links. So my link code is like this:
<i class="fas fa-link fa-lg"></i>
The page "lenker.php" does a search and outputs html code. It looks fine in the chrome inspector. But the popup is empty. If I link to another page with the code hardcoded it shows fine. Why does it not show when it is generated on the fly? The html code looks just fine like this:
<div><a class="external" href="http://databank.artsdatabanken.no/FremmedArt2012/N63753" data-featherlight="ajax">fremmedartsvurdering 2012 for edelgran</a></div>
<div><a class="external" href="http://eol.org/pages/1033070" data-featherlight="ajax">edelgran hos Encyclopedia of Life</a></div>
<div><a class="external" href="http://linnaeus.nrm.se/flora/barr/pina/abies/abiealb.html" data-featherlight="ajax">edelgran i Virtuella floran, Sverige</a></div>
but the popup opens and then resizes to almost nothing since there is no content. Featherlight does not need any other initialization since it looks for data-featherlight="ajax".
Is the problem that datatable is dynamic content?
jQuery ajax expects one object so I solved this with enclosing my content in one DIV.

How to get Inspect Element code using Selenium WebDriver

I'm working in selenium with Firefox browser.
The Html code shown in View Source (CTRL+U) is different from the html code i see when inspecting the elements in Firefox.
When i run the driver.getPageSource() i only get the View source (CTRL + U) codes.
Is there is any way to access the Inspect element code instead of View source code?
I think your question is answered here.
The View Source html is what is sent by the server. I think of it as compile time html, or the initial state of the DOM.
The Inspect Element html could have been updated by ajax responses or javascript so will not necessarily be the same. I think of it as runtime html, or the current state of the DOM.
The GetAttribute() method queries the current DOM element state. You can return a particular html attribute value directly
webElement.GetAttribute("class")
or get the whole html string.
webElement.GetAttribute("innerHTML")
There are some fundamental difference between the markup shown through View Source i.e. using ctrl + U and the markup shown through Inspector i.e. using ctrl + shift + I.
Both the methods are two different browser features which allows users to look at the HTML of the webpage. However, the main difference is the View Source shows the HTML that was delivered from the web server (application server) to the browser. Where as, Inspect element is a Developer Tool e.g. Chrome DevTools to look at the state of the DOM Tree after the browser has applied its error correction and after any Javascript have manipulated the DOM. Some of those activities may include:
HTML error correction by the browser
HTML normalization by the browser
DOM manipulation by Javascript
In short, using View Source you will observe the Javascript but not the HTML. The HTML errors may get corrected in the Inspect Elements tool. As an example:
With in View Source you may observe:
<h1>The title</h2>
Whereas through Inspect Element that would have corrected as:
<h1>The title</h1>
getPageSource() always returns the markup obtained through View Source.

Selenium: How to get plain text from html source?

i'm going to rewrite all my tests project, by replacing Selenium by HtmlUnit because i'm not able to get plain text in selenium as i can do with htmlunit using "HtmlPage:asText" method. Getting plain text can help me to verify easily the content of a page, without paying attention of the presence or not of the tags.
For example a plain text like this " One, two three" may correspond to many html source:
<p>One, two three</p>
or <table> <tr><td>One1</td><td>two</td><td>three</td> </tr></table>
or <div><span>One, </span> <span>two, </span> <span>three, </span> </div>
By using HtmlUnit i can write functional test without paying attention of how the actual content will be represented in the html format.
This will give you only plain text in page
String pageSource=driver.findElement(By.tagName("body")).getText();
Below logic will gives you entire page source.
driver.getPageSource();