Robotframework Selenium Library - extract clas sub-element - selenium

I need to extract an attribute "sub elemement" after initial selector.
I'm using this syntax as selector:
${first_video}= Get WebElement xpath:(//div[contains(#data-store, 'videoID')])[1]
This correctly give me this single element part:
<div class="alpha" data-store="{"videoID":"1234","playerFormat":"inline","playerOrigin":"video_home","external_log_id":null,"external_log_type":null,"playerSuborigin":"entry_point",
"useOzLive":false,"playOnClick":true,
"videoScrollUseLowThrottleRate":true,"playInFullScreen":false,"type":"video","src":"https:\/\/video.test.com\/v\/a\/123_n.mp","width":320,"height":180,
"videoURL":"https:\/\/www.test.com\/test\/videos\/12345\/","disableLogging":false}" data-sigil="Video">
</div>
My goal is to get the VideoURL attribute.
I've tried with
${first_video_link}= Call Method ${first_video} value_of_css_property data-store.videoURL
but it gaves me empty output.
Is there any specific syntax that I'm missing?
Many Thanks

Get the value of the data-store atrribute:
${val}= Get Element Attribute ${your locator}
It appears to be a proper json, so convert it to a python dictionary and then just access its key:
${as json}= Evaluate json.loads("""${val}""") json
Log To Console The videoURL is ${as json['videoURL']}

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 select one from duplicate tag in page in java in selenium webdriver

I am using Selenium WebDriver and I have number of items on a page and each item on page is a separate form type.
I have saved all of these form elements in a list and I am iterating over every item in an attempt to get the name of the element by using the "alt" attribute.
However when I try to get the "name" attribute from the input element it is always returning the first input tag found on that page, not the name attribute of the element I have currently selected.
The syntax I am using is:
((Webdriver imgtags.get(i)).findelement(By.xpath("//input[#name='qty']")).sendKeys ("100");
I have also tried to get the id from the tag by using:
((Webdriver imgtags.get(i)).getAttribute("id");
It's returning a blank value, but it should return the value of the id attribute in that input tag.
I also tried to get the id by using .bytagname but as id is an attribute it is not accessible
Try:
(driver) findElement(By.xpath("//*[contains(local-name(), 'input') and contains(#name, 'qty')]")).sendKeys("100");
To answer the comment by #rrd: to be honest, I have no idea why OP uses ((Webdriver imgtags.get(i)). I don't know what that is. Normally, I just use driver.findElement[...]
Hoping that he knows what works in his framework :D
Selenium Xpath handling is not fully compliant and it does not always treat // as a synonym of descendant-or-self.
Instead try tweaking your code to use the following Xpath:
((Webdriver imgtags.get(i)).findElement(By.xpath("./descendant-or-self::input[#name='qty']")).sendKeys("100");
This will base your search off the currently selected WebElement and then look for any descendants that have a name attribute with a value of "qty".
I would also suggest storing your imgtags array as an array of WebElement e.g.
List<WebElement> imgtags = new ArrayList<>();
This is a much better idea than casting to WebDriver to be able to use .findElement(). This will cause you problems at some point in the future.

Unable to locate list elements using Nightwatch

I have some list elements:
< button id={"productBook"} className="fito-btn fito-btn-long" style={this.props.styles.btnBrandRevers} onClick={this.props.onOfferSelect.bind(null, product)}>
< FormattedMessage id='app.Book' defaultMessage='Book' />
< /button>
When asserting the element with id productBook as:
.assert.visible('button[id=productBook]')
I'm getting an error:
Testing if element <button[id=productBook]> is visible. Element could not be located. - expected "true" but got: "null"
I don't understand why this doesn't work for this specific element while it works for other elements. Is there some different way that list elements need to be verified?
Please help.
Try using the shorthand # for id:
assert.visible('button#productBook')
or
assert.visible('#productBook')
As per the HTML you have shared the id attribute of the <button> is dynamically generated. So you may not be able to assert through id. Instead you can assert the visibility of the FormattedMessage through the following Locator Strategy :
xpath :
//button[#class='fito-btn fito-btn-long']/FormattedMessage[#id=\"app.Book\"][#defaultMessage='Book']
Try the following:
.assert.not.elementPresent('tr[...]')

SyntaxError: The expression is not a legal expression

I don't know how I can use any elements to get clicked on this submit button.
I've tried it using xpath.,
web.find_elements_by_xpath('\\input value="submit" type="submit"').click()
but I get the error metioned in the title.
I searched and I couldn't find any solution.
Here's the inspected element by the browser:
What you pass to find_elements_by_xpath() doesn't looks like XPath. Also find_elements_by_xpath() should return you a list which doesn't have an attribute click(). Try to use below instead:
web.find_element_by_xpath('//input[#value="submit" and #type="submit"]').click()

webElement.getAttribute() is not working in selenium

I am using selenium for automating test cases for web application in that I have to get tool tip text
I tried
String result =element.getAttribute("span");
but this is retuning null. how can I get the text ?
element.getAttribute("span");
span must not be an attribute of the given element. I guess you've misunderstood, the getAttribute() method.
For e.g.
Example
For the above anchor tag, in order to get the title attributes' value, you can use :
element.getAttribute("title");
where element refers to the above anchor element.