Get href from an element found by ID - Python - Selenium - html

This is my code:
dispatch_button = browser.find_elements_by_xpath("//*[contains(#id, 'alarm')]")
href = dispatch_button.get_attribute('href')
print(href)
This is the HTML i'm working with:
Dispatch
The ID and href both contain a number that changes which is why i'm finding it using the partial ID.
I'm completely stumped as i can't see why I'm getting this error:
href = dispatch_button.get_attribute('href')
AttributeError: 'list' object has no attribute 'get_attribute'
I'm trying to locate the dispatch_button by its partial ID, and then use its href.

If you want to get single WebElement you should use find_element_by_xpath() instead of find_elements_by_xpath():
dispatch_button = browser.find_element_by_xpath("//*[contains(#id, 'alarm')]")
href = dispatch_button.get_attribute('href')
or if you want to handle multiple elements matched by specified selector:
dispatch_buttons = browser.find_elements_by_xpath("//*[contains(#id, 'alarm')]")
hrefs = [dispatch_button.get_attribute('href') for dispatch_button in dispatch_buttons]
Also you can try to locate required anchor by link_text:
dispatch_button = browser.find_element_by_link_text("Dispatch")

Related

Calling Select Tag from URL

I have a webpage with a <select> tag called dropDown with selectable options 'OptionA', 'OptionB' and 'OptionC'.
It is possible for me to link to the webpage and call one of the options from the URL?
For example: smaple.aspx?dropDown=OptionB
You can get the GET parameter with JavaScript
let params = new URLSearchParams(document.location.search);
let dropDownValue = params.get("dropDown");
then change the select value with JavaScript(assume the select element has a id called a-dropdown)
document.getElementById('a-dropdown').value = dropDownValue;

element.value method is returning undefined

I have an ajax 'POST' method that sends the id input to a php file. For some reason whenever I write input.value method, it returns undefined:
input = document.getElementsByClassName("Input");
const id = input.value;
alert(id);
What am I doing wrong?
Edit: I tried making the element as a separate id instead of a class and the problem disappeared.
getElementsByClassName() returns an array-like collection of elements, not a single element.
You'll need to extract one of the elements from the collection, e.g.
input = document.getElementsByClassName("Input");
const id = input[0].value; //<--
alert(id);
Better would be to target the exact element in some way e.g.
document.querySelector('#theActualElement'); //<-- returns single element

Get data-id from html element

I'm trying to extract the content of data-id.
For example :
<div data-id= "43434"></div>
How can I get the value of 43434? I want to get access to the content of data.
As I see you want to get this value inside a TestCafe test.
If so you can use the Selector.getAttribute() method.
const element = Selector('your-div-selector');
const attrValue = await element.getAttribute('data-id');
// or if you need to use it in an assertion
await t.expect(element.getAttribute('data-id')).eql('43434');
Get the element using has attribute selector and get the value from dataset property or get attribute value using Element#getAttribte method.
console.log(
document.querySelector('div[data-id]').dataset.id
)
<div data-id="43434"></div>

Href attribute empty when selecting anchor with xpath

I have a number of links in a page that look like so :
<a class="plant_detail_link" href="plants/O7-01111"><h3>O7-01111</h3></a>
I can select all these link in my page with the following xpath :
//a[#class='plant_detail_link']
I can extract attributes like the class of each link in the usual manner :
//a[#class='plant_detail_link']/#class
But when I attempt to use the same technique to extract the href attribute values I get an empty list :
//a[#class='plant_detail_link']/#href
Does anyone have any ideas why this may be the case?
image detailing chrome developer console xpath execution
EDIT:
See full page html here - http://pastebin.com/MAjTt86V
it's a chrome bug, I believe. You can add the [index].value to get the result. In other words, the $x for href did work but it doesn't return the result in the output for some reason.
For example, I ran these $x queries in the console on this page for the 'Questions' button and got the following output:
$x("//a[#id='nav-questions']/#href")
> []
$x("//a[#id='nav-questions']/#href")[0].value
> "/questions"
You can use something like this to get a usable array of values:
var links = $x("//a[#target='_blank']/#href");
var linkArr = [];
for (i in links) { linkArr.push(links[i].value)}
or to put it in a function:
function getHref(selector, value, $x) {
var links = $x("//a[#"+selector+"='"+value+"']/#href");
var linkArr = [];
for (i in links) { linkArr.push(links[i].value)};
return linkArr; }
getHref("target","_blank", $x);
EDIT
Not sure if this will help you but in chrome adding a comma like this returns the output without the [index].value:
$x,("//a[#id='nav-questions']/#href")
> "//a[#id='nav-questions']/#href"
you could try adding a comma to the xpath selector but I'm not sure if it will help in your case.

How to set parameter and its value to HREF dynamic using javascript

I have a link like following
<a id="dynamicLink" href="http://www.w3schools.com?userName=test">Visit W3Schools</a>
I would like to change the value of userName from test to test1.
How to do that using javascript?
could someone help me on this?
I suggest using a library to work with URIs. It will provide some consistency. Here is an example using URI.js:
// Get our <a> element
var l = document.getElementById('dynamicLink');
// Use URI.js to work with the URI
// http://medialize.github.io/URI.js/
var uri = URI(l.href);
// Get the query string as an object
var qs = uri.query(true);
// Change our value
qs.userName = 'test1'
// Update the URI object
uri.query(qs);
// Set our new HREF on the <a> element
l.href = uri;
<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.15.2/URI.min.js"></script>
<a id="dynamicLink" href="http://www.w3schools.com?userName=test&someOtherKey=val">Visit W3Schools</a>
Try this:
document.getElementById("dynamicLink").href.replace("userName", "test1");
This should change the value of userName in href to test1