Getting Attribute using xpath in selenium - selenium

I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. Can anyone please help
the attribute value is dynamic changes for every next webelement
below is the HTML code
<figcaption id="recentVideosvideoname0" data-videono="xZA6FJ32Twe2GQYEuBHJnQ==" title="test for test" class="caption">test for test</figcaption>
i want attribute : data-videono
I have tried something like this
By.xpath(("//div[contains(#id,'recentVideos')]/div/a/figure/figcaption"));

Here is the Answer to your Question:
To retrieve the attribute data-videono you can consider to use the following line of code:
String my_attribute = driver.find_element_by_xpath("//figcaption[starts-with(#id, 'recentVideos') and #title='test for test']").getAttribute("data-videono");
Let me know if this Answers your Question.

Please follow the approach mentioned below:
driver.findElement(By.xpath("//div[contains(#id,'recentVideos')]/div/a/figure/figcaption")).getAttribute("data-videono");

You can get attribute value by using XPath itself.
XPath 1.0
"//div[contains(#id,'recentVideos')]/div/a/figure/figcaption/#data-videono"
Notice the #data-videono at the end of string, which will return attribute value (p.s. it seems like it is base64 encoded data)

I don't know if i got your question, but do you already tried:
String var = driver.frindElemnt(By.xpath("put your xpath in here")).getAttribute("data-videono");

Related

How can you use "getAttribute" method of WebDriver in HTML structure?

How can you use "getAttribute" method of WebDriver for the given HTML structure?
<span id=“span1” class=“spanClass” > Option1 </span>
Code trials :
driver.findElement(By.id("span1")).getAttribute("class");
and
driver.findElement.getAttribute(By.id("span1"));
and
driver.findElement("value").getAttribute(By.id("span1"));
and
driver.findElement(By.id("span1")).getAttribute();
your first trial,
driver.findElement(By.id("span1")).getAttribute("class");
looks good, did you try printing that ? if yes, did you get proper output or was there any other error ?
your 2nd code trial
driver.findElement.getAttribute(By.id("span1"));
is invalid, since you can not call getAttribute() like that, it should result in compile time error.
Your 3rd code trial :
driver.findElement("value").getAttribute(By.id("span1"));
is again invalid, you can not find the element with a string input.
your 4th code trial :
driver.findElement(By.id("span1")).getAttribute();
is again invalid, since you are not passing anything in getAttribute();
method. It should again result in compile time error.
Below is an alternative way with xpath :
//span[#id='span1']
and get the id or class attribute like this :
String idAttribute = driver.findElement(By.xpath("//span[#id='span1']")).getAttribute("id");
System.out.println(idAttribute)
or to get class attribute you can do something like this :
String classAttribute = driver.findElement(By.xpath("//span[#id='span1']")).getAttribute("class");
System.out.println(idAttribute)

How to retrieve the label text as per the html provided?

Sometimes I failed to get the inner text of a web-element; recently working on thePersonal insurance, and failed to get inner text of label (web element). Here are the script and screenshot of webpage inspected:
WebElement detailPostCode =driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
System.out.println("postcode label text "+detailPostCode.getText());
Could any please help me understand the problem. Thank you for your kind concern.
As per the HTML you have shared the <label> with text as Postal code is a child node of the <div> tag with class attribute as q_codePostal.
What went wrong?
As per your code trial you have used:
WebElement detailPostCode = driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
In this expression //label[#for='q_codePostalDetailAU'] will always refer to the descending <input> tag with id attribute as q_codePostalDetailAU. Hence your code trial didn't work.
Solution
As an alternative you can use the following solution:
WebElement detailPostCode = driver.findElement(By.cssSelector("div.q_codePostal>label"));
Can you try this xpath:
driver.findElement(By.xpath("//div[#class='q_codePostal']/label"));

WebDriver find by xpath where //input[#value='empty']

I need to find an input element with an 'empty' value by xpath.
Something like //input[#value='empty']. I don't mean the word 'empty', but the value is empty or blank.
What Xpath will return such element?
Thanks
Since the link I posted in the comment is a potential solution
So, I thought to post it as an answer:
How do I select an empty element in XPath?
Edit:
Based on advice from the comment to duplicate the essential parts of the linked answer (incase the link becomes obsolete), please find it below:
You could use XPath's not() function.
a[#href='#'][#class='button save-as'][#title='SaveAs...'][not(text())]
Next time post html snippet so that people will understand parent and target element
the below code will work, but if more input fields with null values are there then more than one result will be returned. [i am sure below xpath will return many results]
so please include parent node in below xpath
//input['not(text())']
the input belongs to text area, submit button, radio button , check box ....
again, post the HTML snippet next time. when you ask question related to xpath.

Not able to identify a lenghty linktext in selenium webdriver

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution
Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click
try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.

Selenium select element with multiple attributes

How do I select a div using both it's id and class in Selenium firefox addon?
EDIT: I managed to solve it, I used an xpath expression with both the attributes #id= and #class =
I stumbled upon this question and thought I would leave an example of the answer that the OP didn't leave (as requested by vincebowdren above):
//*/fieldset[#class="openable"][#id="activityFieldset"]
This would select a fieldset element with the openeable class and activityFieldset id.
I would like to post another correct answer for future reference.
First of all there have been other correct answers here:
https://stackoverflow.com/a/42996525/6569715
A valid example would be to define a locator by multiple attributes:
HEADER_1_TEXT = (
By.XPATH,
"//h2[#class='text-primary' and #id='my-id' and text() ='Get started with Us']")
I managed to solve it, I used an xpath expression with both the attributes #id= and #class =