Python 2.7 Copy and paste hyperlinked text - html

I am using Python 2.7, Webdriver and Chrome. Manually, I can mouse swipe across text containing a hyperlink on a web page and copy it to the clipboard. How do I do this automatically? I have no issue finding the element containing the hyperlink. I am not trying to find the hyperlink. I am trying to paste it into a web page text box which does not process https://www.python.org/ ">Link within an "a" tag but processes it correctly when pasted from elsewhere i.e. "Link" with embedded href.

Even after OP clarifications, it's still hard to understand the exact issue, so I'll try to cover all possible options :)
Suppose we have an anchor element, like Link
We can find this element in such ways
element = driver.find_element_by_xpath('//a[text()="Link"]')
element = driver.find_element_by_xpath('//a[#href=" python.org "]')
depending on what information we currently know about the element and what exactly we want to scrap.
Also, we can use index of anchor element element = driver.find_elements_by_tag_name('a')[0]
1) To get value of href attribute:
value = element.get_attribute('href')
Output: https://python.org
2) To get value of text node:
value = element.text
Output: "Link"
3) To get complete HTML code of element:
value = element.get_attribute('outerHTML')
Output: Link

Related

How can I change the href text using javascript im stuck

i got stuck 4 hours ago and i cant figure this out, how can i make the balance to go from 0.13 to $50.13?
Here is the code:
<li class="notranslate" id="Mata" translate="no">
Balance: $0.13</li>
Im trying to do this using the console in google chrome
If you set an id on the link like this:
<a id="link" href="/wallet">Balance: $0.13</a>
Then you can use:
document.getElementById("link").innerHTML = "Balance: $50.13";
If you are not able to set an id then you can do:
document.querySelector("link[href='/wallet']").innerHTML = "Balance: $50.13";
The chrome inspect tool also has a feature where if you click the icon in the top-left of the tool, it will let you select an element from the page. After selecting an element, you can refer to it in the console as $0. So you after selecting the link you could do:
$0.innerHTML = "Balance: $50.13";
Since you do not have an id assigned to your a tag, you can use document.querySelectorAll, which will return a list of elements that match the specified selector.
Then, you can access each element using its index number, which in your case is 0, since you have only one element.
To change the text, simply use the text property:
document.querySelectorAll("a[href='/wallet']")[0].text = "Balance: $50.13";

Rselenium and object with wbr

I am trying to use RSelenium to navigate with Firefox. I need to change an option button in about:config page for parameter browser.helperApps.neverAsk.saveToDisk. With RSelenium I am able to find the element I need to modify, but then I cannot find a way to update the value.
This is my HTML string to find
<tr class=""><th scope="row">browser.<wbr>helperApps.<wbr>neverAsk.<wbr>saveToDisk</th><td class="cell-value"><form id="form-edit"><input type="text"></form></td><td class="cell-edit"><button data-l10n-id="about-config-pref-save-button" class="primary button-save" title="Salva" form="form-edit"></button></td><td class="cell-reset"></td></tr>
I am using
go4 = rsc$findElement(using = "xpath", value = "//th[substring-after(normalize-space(string(.)),': ')=browser.helperApps.neverAsk.saveToDisk']")
but the error I get is
Summary: NoSuchElement
Detail: An element could not be located on the page using the given search parameters.```
How can I find the element and change the default value?
Thank you

Using a tag in a dynamic url

I'm struggling to get the contact ref to work from different pages.
Basically for every product a different detail reference is provided (see 4867820 below).
I would like to create a button which links to the #contact id for every page on the website.
Basically if I put a link on the button including the reference, this works perfectly.
http://www.mywebsite/product/4867820#contact
However I am wondering if I can create something similar where the button links to the contact id on the same page without using the reference number.
http://www.mywebsite/product/this page#contact
So basically my question is how do I link to only #contact on this page without the reference numbers, so I can create 1 button on the page template.
Regards,
Assuming it will always point to the same page as the button:
a) If you are able to use anchor tags instead of buttons (and CSS-style them to look like buttons), you can simply point the anchor's href attribute to the section name.
It works like a relative link, just appending the href to current URL. Like so:
Contact Here
Or b) if you have to use only a <button> or <input> element specifically, you will need to use javascript to set the location in onclick handler of the button. For details on that see: Javascript inline onclick goto local anchor

How can I navigate using anchor tag in SAPUI5?

I know how to do using html and JavaScript
<h2 id="C4">Chapter 4</h2>
Jump to Chapter 4
This is what I am trying in SAPUI5. On click to Back to top link it should navigate to helpButton. This is not working for me.
<Button id="helpButton" icon ="sap-icon://sys-help" />
<Link text="Back to top"
press="#helpButton"/>
You can actually do this in UI5. A little differently than how you tried though.
The problem is that the UI5 ID is not the same as the HTML ID (which is what you want to use with the hash-link for the browser to jump there). Also, you cannot specify a URL inside the press "attribute" of the link. The press "attribute" is in fact an event (so you can only specify an event handler name).
So to be able to do what you want, you have to use the href property of the Link and fill it with the HTML ID of the target control. You can do this on the onAfterRendering hook of the view (that's when you are able to find the HTML ID of the target control):
onAfterRendering: function() {
var oRef = this.byId("target").getDomRef();
this.byId("link").setHref("#" + oRef.id);
}
You can find a working fiddle here: https://jsfiddle.net/93mx0yvt/26/.

Tracking Link Click on Google Tag Manager

I want to track clicks on the following button/link with Google Tag Manager. I created a trigger in Google Tag Manager that triggers when the element_id = 100. This works fine, except that when I click exactly on the text, it doesn't do anything, the link looks like a button, with the text in the middle of it. I can't change anything to the html or css, otherwise I can think of multiple things, so I need to find a solution without changing the html. Also, the 'myclass' class and the 'label' class get used in other elements.
<a class="myclass" id="100" href="http://www.url.com">
<span class="label">Text</span>
</a>
Anyone an idea?
Thanks a lot,
The following workaround worked:
Create trigger when element text contains "Text". This will trigger events on the button and the label on the button, of all buttons with "Text" as label.
Create tag for that trigger that checks with simple javascript if either the id of the current element = 100, which will happen when you click the button but not the label, or that the id of the parent = 100, which happens when you click the label. You can get the element that triggered the tag using the built-in variable "Click Element". Which you need to access the parent element.
Technically, you shouldn't have a CSS ID that starts with (or is) a number, so not sure if your code example is accurate or not. Whatever the case, you're probably better off using "matches CSS selector" so that you don't need to use any custom JS.
If indeed your HTML uses id="100", then the above will work. If it's anything else that doesn't start with a number, then you can use
#whatever > span