I am trying to make a crawler to crawl images on imgur. I am having trouble selecting the element I want to select. I am attempting to select the href link for each image on the home page. The home url is: https://imgur.com/t/memes/
If anyone knows the correct css selector to get the specific href link for each image that would be great!
For further clarification, I need to select the link in the href : here is the html code. Does anyone know how I can do this?
This should do:
a.Grid-item::attr(href)
breakdown:
a - select node.
.Grid-item - that has class "Grid-item".
::attr(href) - select attribute href of current node.
Related
I am working on image click functionality where images change dynamically but the XPath remains the same.
For example:
First image inspect element:
<a id="adlink" target="_top" href="http://www.jimhayes.com/lennielw/index-2.html">
<img id="adimage" src="http://egranary/adverts/Images/lightwire.png" border="0"></a>
First image XPath:
//*[#id="adimage"]
Second image inspect element:
<a id="adlink" target="_top" href="http://www.lifewater.ca/index.html">
<img id="adimage" src="http://egranary/adverts/Images/lifewater1.png" border="0"></a>
Second image XPath:
//*[#id="adimage"]
In this example source and target are changing constantly to redirect to particular page when clicked on image.
How to handle this situation using selenium webdriver?
I think your requirement is to basically check if the links are working fine - not broken.
In that case, I would suggest you to follow this approach - (It is in Java - you can do something similar in other languages as well)
I am trying to extract all href on the page. But if you want an specific xpath just change it here.
driver.get("https://www.yahoo.com");
Map<Integer, List<String>> map = driver.findElements(By.xpath("//*[#href]"))
.stream() // find all elements which has href attribute & process one by one
.map(ele -> ele.getAttribute("href")) // get the value of href
.map(String::trim) // trim the text
.distinct() // there could be duplicate links , so find unique
.collect(Collectors.groupingBy(LinkUtil::getResponseCode)); // group the links based on the response code
Now we could access the urls based on the response code we are interested in.
map.get(200) // will contain all the good urls
map.get(403) // will contain all the 'Forbidden' urls
map.get(404) // will contain all the 'Not Found' urls
map.get(0) // will contain all the unknown host urls
Check here for complete implementation.
I'm working on editor where user can set link to another website , for example stackoverflow.com. If user set this url to <a> element and then click on this link he will be navigated to http://myapp.com/stackoverflow.com but not to http://stackoverflow.com. I supposed that this is caused by changed default behavior of element.
How can i make this link external? if user will copy url from browser and paste he will paste http://stackoverflow.com and hence everything will works fine. But in case he manually enter stackoverflow.com nothing will work fine.
P.s. i also would like to make each element with attribute target="_blank" so, i assume that i can't navigate programatically
Any ideas?
Try setting the <a>'s href attribute to the full url, not just stackoverflow.com:
<a href="http://stackoverflow.com" />
Otherwise it's treated as a relative url.
Add a check to see whether your users input the url as stackoverflow.com, and if they do, change it to http://stackoverflow.com before setting the href attribute.
Didn't get your other question about programmatic navigation, but if you want a link to open in a new window/tab, use
<a href="http://stackoverflow.com" target="_blank" />
Is it possible to set ng-href to go to the current page?
eg:
<a ng-href="https://www.facebook.com/sharer/sharer.php?u={{ window.location.href }}">facebook</a>
When the above runs, I keep getting:
<a ng-href="https://www.facebook.com/sharer/sharer.php?u=" href="https://www.facebook.com/sharer/sharer.php?u=">facebook</a>
How are you setting value to window.location.href ? This sure doesn't look like native JS.
Here's a fiddle to help you out.
ng-href is part of AngularJS and there are a few ways to point to the same page. The method I use do not include the domain so the Angular Router will direct it as needed, like the following.
ng-href="/mySubDomain"
The Docs go into detail about this and give a nice code sample that shows you what you should expect from the route change.
https://docs.angularjs.org/api/ng/directive/ngHref
I need help to open link while scraping the www.apartments.com website.The link is actually a pagination link that is populated by javascript/jquery.
Here is how the link looks like:
<a class="pagination-link" data-page="2" href="#">2</a>
This my selenium script to find and click the link:
pagination = browser.find_element_by_link_text('2')
pagination.click()
Selenium does find the link and click. Another opened but it's not the page 2. I know that happens because the href="#" points to same page.
Any help will be appreciated.
It's a good idea to give full examples of your code and fully explain the errors you're getting. Having said that, here is my stab at what you should do. C# example, adjust as necessary:
Note: this is using a CSS selector, rather than link text
int pageNumber = 2;
IWebElement paginationLink = _webDriver.FindElement(By.CssSelector(".pagination-link[data-page='" + pageNumber + "']"));
paginationLink.Click();
I have a page /hello where i have a link:
<a href="{{#url 'goodbye' }}{{/url}}">
that will redirect to /goodbye. But i want it to use an anchor too, something like /goodbye#message
I have tried doing:
<a href="{{#url 'goodbye' }}{{/url}}#message">
but when I click on it, it will redirect the page to /goodbye. It seems like Chaplin is deleting the anchor.
EDIT:
For the templates I'm using handlebars (with the chapling boilerplate), the {{#url}} helper generates correctly the link ( cf view-helper.js ). In the rendered page i see:
<a href="/goodbye#message">
but when i click on it, it just redirects me to /goodbye
Any idea?
Found a solution, I needed to stop the routing on the link. I just added the class noscript on the tag
<a href="{{#url 'goodbye' }}{{/url}}#message" class="noscript">
I couln't find a different way to do it. Hope this helps someone else
cf : skipRounting on Chaplin.Layout