Xpath query not working in Objective C using GdataXML - objective-c

I am trying to parse an xml using GdataXML
I have a query which is not working and I wonder why:
<address_component>
<long_name>Mars</long_name>
<short_name>Mars</short_name>
<type>route</type>
</address_component>
I want to save the long_name where the type is route and I am using this query
/*[name() = 'address_component']
/*[name() = 'type' and text() = 'route']
/*[name() = 'long_name']
What am I doing wrong here?
===========
Is there a way for GDataXML to parse the xml line for line?
is there a way other than running the xml a few times into an array?

I want to save the long_name where the
type is route
This XPath expression should work:
/address_component[type='route']/long_name
Meanning: a long_name element child of an address_component root element having a type child element with 'route' string value.

Related

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.

Getting Attribute using xpath in 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");

clicking on dynamic values in a web table in selenium

Below is my Table structure:
I want to click on the first cell of "policyno " column only if value is not empty
How do I achieve this?
Since you didn't specified, I have no idea what programming language do you need, but the idea is the same for all. I've used C# for the code below:
Get all the policy number elements. This can be easily achieved by getting the elements by xpath. In your case, I expect the lblPolicyNumber to be present in all:
IList allPolicyElems = driver.FindElements(By.Xpath(".//*[contains(#id,'lblPolicyElements')]"));
Now, you have 2 options to click on the element you need. First, you click on an element using his position in the list: allPolicyElems[0].Click(); (not the best way to do it) or by using LINQ (or lambda expressions for Java) to get the element by the text (perhaps you have the text stored in a variable from some previous actions): allPolicyElems.FirstOrDefault(t => t.Text == "your_text_here").Click();
This can be further expanded by applying the same logic in case you need to get the correct element by knowing other cell values from the same row.
Try using this.
WebElement elem = driver.findElement(By.xpath("//table/tbody/tr[2]/td[3]/span/a"));
if(!(elem.getText().trim().equals(""))){
elem.click();
}

Is there a shorter syntax than soup.select("#visitor_stats")[0]?

I'm using BeautifulSoup (import bs4) to read some information from a web page. Several lines in my script look like
stats = soup.select("#visitor_stats")[0]
Is there a shorter syntax for this?
select() lets you select a bunch of HTML tag elements based on their CSS properties (like id and class). In this case you are looking for all HTML tag elements with CSS id property set to visitor_stats. And then selecting the first element from the returned list.
The BeautifulSoup method find() returns the first occurrence of the search criteria. So the list index [0] can be gotten rid of by using find()
stats = soup.find(attrs={'id':'visitor_stats'})
But I am not sure if this is any shorter :)

How to test XELement and its child elements?

I have a sample Xml code snippet
<modification name="givenName" operation="add" xmlns="urn:oasis:names:tc:DSML:2:0:core">
<value>Changed name</value>
</modification>
The xml is loaded to my XElement, and I used
XElement xml = ...to load xml above...;
xml.Should().HaveAttribute("name", "givenName")
.And.HaveAttribute("operation", "add")
.And.HaveAttribute("xmlns", "urn:oasis:names:tc:DSML:2:0:core")
.And.HaveElement("value");
to test my code, the attributes testing are all passed, but the element testing (the last condition) fails.
Anyone can point out what is wrong with my code?
And how can I test the Xml has an element named "value" and its value is "Changed name"?
Thanks in advance!
I suspect the problem is that the XName of the element isn't just value - it's value with a namespace. Presumably HaveElement is namespace-aware. Try this:
XElement xml = ...to load xml above...;
XNamespace ns = "urn:oasis:names:tc:DSML:2:0:core";
xml.Should().HaveAttribute("name", "givenName")
.And.HaveAttribute("operation", "add")
.And.HaveAttribute("xmlns", "urn:oasis:names:tc:DSML:2:0:core")
.And.HaveElement(ns + "value");
The last line checks whether it's got the namespace-qualified element.
It's going to be part of Fluent Assertions 2.1. If you can't wait you can get it through the Git repository