Detect if selected element is an anchor on right click - javascript

I'm writing a small firefox addon to grab urls and send them to a site. What I want to do is be able to right click on a link (but not have to actually highlight it) and be able to click send and the links href value is grabbed and sent. The bit I'm having trouble with is detecting if the selected element is an anchor and grabbing it's href. Many thanks for any help given :)

nvm, found the solution at:
https://developer.mozilla.org/en/XUL/PopupGuide/Extensions#Determining_what_element_was_context_clicked

Related

Create an anchor without having it displayed in the url

So I have this page which has 3-4 anchors and whenever I click the button to jump to the section I want, the url should remain the same (without adding #anchor).
I googled this but I couldn't find anything that works and I'm still learning JS so I don't have the knowledge to do this.
Not sure what you mean, Perhaps you can edit your question to show us what you have already tried. Anchors are how the browser knows where to scroll to, if you want to achieve the same thing but without changing the anchor in the address bar you can try something like this on click:
elmnt.scrollIntoView();
See also: https://www.w3schools.com/jsref/met_element_scrollintoview.asp

JavaScript - Selenium Webdriver. How to check if element is visible

Im using Selenium to open a page. After I open the page, I want to click a some sort of refresh button. When I click it, Im getting this error:
ElementNotVisibleError: element not visible
This is the code I use to click the button:
driver.findElement(By.id(id)).click();
My guess is that the button goes not visible whenever it is pushed. And that maybe it starts off not visible as well. Cause it doesnt do a full page refresh.
Any way for selenium to check if something is visible, and maybe wait until it is?
Edit:
As it turns out, there are multiple buttons with the same ID. And the button I am trying to reach are way down. I tried to find the button with By.xpath, but I still couldnt find it. The way I did it, was to search for button by id, like this //button[contains(#id, 'abc')][1]. (Different number of course).
Selenium could not find the element, but I could find it with Chrome developer tools. Any suggestions?
Solution: 1 You can try using this code in a syso,
System.out.println("getting Page Source "+driver.getPageSource());
This method will return the entire page Source and you can check whether your button exists in the source or not. You can place the above code at several points in your function and check until you find the button in the source.
Solution: 2 In case your button does not exist in the source you will have to check and see whether the button is getting loaded in an iframe and if so you can use the following code and switch to the iframe and and then try finding the button,
driver.switchTo.frame(frameName);
In your case you could use Explicit wait to check when the element/button is clickable.
Try doing this
wait.until(ExpectedConditions.visibilityOfElementLocated(id))

Webscraping automation How to simulate click of HREF link where no link TAG is there?

I am trying to scrape/automate a certain website but struggling to know where to start - I know you can point me in the right direction here - in my example - selecting a horse odds.
If you follow this link http://www.oddschecker.com/horse-racing and then click on any of the races, I want to be able to automate on a race meeting - the user clicking of the odds of one of the horses at one of the bookmakers but there is no anchor or HREF tags or any link for me to tap into programmatically.
Here is an example of what inspector throws up:
<td class="bc bs o" data-odig="5.5" data-o="9/2" data-hcap="">5.5</td>
I want to be able to simulate the user clicking on that 5.5 in the TD.
Many thanks.
Simon
If you can click on this and it actually fires an event, then there is javascript listening to that event. You can get the Javascript to execute with JQuery:
$("td [data-odig='5.5']").click();
(Obviously the selector should be updated for whichever element you want to click.)
Reference: https://api.jquery.com/click/
So you need to be able to run your own arbitrary JavaScript in the page, and then you can accomplish this.

Scroll to position after reload

I'm not that into Javascript but I think this could be achieved easily.
I've got a link on my page that sets a parameter (with PHP). That parameter is used to get how much posts to show. This works like a charm.
But now I want to scroll to the postition where the user clicked the link after the page reloads with the parameters. So the users doesn't have to scroll through the posts he already saw.
So here are the steps:
User clicks link to load more posts
Site gets reloaded with parameter (e.g. domain.com/?numberofposts=10)
Now the Site should scroll to the position the users was when he clicked the link
I was trying to achieve that with scrollTo etc but I cant get it working. My thaughts were to pass the scrollposition as an other parameter maybe?
Do someone know how to solve this? Thank you :)
By including an anchor tag within a post or page, you can place links in the body of your post which when clicked allow the reader to jump to another location on the page.
The anchor tag will consist of two HTML elements. First, you'll want to create the link.
If you are linking to a spot on the same page, the format of the link will be similar to:
Link Text
For example, if the text is "Read more about raptors!" then your HTML should look like this:
Read more about raptors!
The above anchor link only works when you are jumping to a specified spot on the same webpage. If you want a link to jump a specific location on a different page, you'll need to replace #anchor with the full URL for the page, similar to:
Link Text
The second part of an anchor tag is the actual anchor. The anchor should be placed at the beginning of the line where you want to start reading after you jump similar to:
<a name="anchor"></a>
Following our previous example, the anchor code will be:
<a name="raptors"></a>
Full tutorial here
You need to know where you have to scroll to.
So yes, you do need to store the scrollposition somewhere (or pass it as a parameter).

Extracting url from anchor tag to use in right click menu

Hey so I set up a right click menu on my site you can see here
http://www.jaminproud.com
Everything works good except I want to add the abilty to open a link in a new tab. I have looked everywhere and cant find a solid answer for this. I would also need a way to make the menu so that option is only displayed while hovering over an anchor tag. Thanks in advance for any help.
P.S I dont need the actual code written that would be nice but rather just to be pointed in the direction Thanks.
There is now way to specify opening in a new tab yet, but when new browsers support CSS3 we will have the ability to tab link.
See: http://www.w3.org/TR/2004/WD-css3-hyperlinks-20040224/#target-new
For now, why not just use target="_new" ?
You need to save the element that was clicked on in the click event handler from the event object's target property. You can get the tag name using the element's nodeName property, and if it's an A element, you can get the URL using the href property.
Tabs are browser specific, so you probably just want to open it in a new window by specifying the target attribute in the link like so:
<a href='somewhere.html' target='_blank'>My Link</a>
Most tabbed browsers will let the user specify if they want it opened in a new tab or a new window when one of these type of links is clicked.

Categories