Selenium select element with multiple attributes - selenium

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 =

Related

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");

Unable to select an element in the table

I am testing out Selenium recently to see if it can recognize my web app better than QTP. So far it seems doing quite well. I ran into a problem trying to find an element within the table element. Some how I was not able to find master table but not the rows within the table.
This is how the table looks like
The code below works fine...
WebElement BaseTable = driver.findElement(By.id("table_simpleBrowser|type=TradingInstrumentReport|!browser"));
Where as the code below does not...
BaseTable = driver.findElement(By.id("table_simpleBrowser|type=TradingInstrumentReport|!browser_tr_1"));
or
BaseTable = driver.findElement(By.className("even status_DEFAULT"));
or
WebElement BaseTable = driver.findElement(By.id("table_simpleBrowser|type=TradingInstrumentReport|!browser"));
BaseTable = BaseTable.findElement(By.className("even status_DEFAULT"));
Can someone please help to show me how I can retrieve the a certain value in the table by finding the element in certain row/column in the table?
Thanks.
even and status_DEFAULT are actually two classes of this web element. By.className() receives only one class as parameter. It should be
findElement(By.className("even"));
// or
findElement(By.className("status_DEFAULT"));
To find element by the two classes use By.cssSelector()
findElement(By.cssSelector(".even.status_DEFAULT")); // note the dot before each class name
However it seems that its not unique enough. I recommend you search by id which contains browser_tr_1
findElement(By.cssSelector("[id*=`browser_tr_1`]"));

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.

How can i select element in selenium webdriver by part of ID.

I have a td element with ID like "a:2:3:d:", and when i want to select it by id, my webdriver can not find it. Is there a way to search by part of ID, because I think, that the problem is at last ":" in the identifier.
First, you need to confirm that this really is the problem, and it's not just that the page isn't fully loaded, or is loaded asynchronously. I don't see any particular reason why Selenium should care about the trailing ":".
Update: From the comments, it's much more likely that the dynamic id that is the problem, but the solution is the same either way:
To find an element by partial id, you can use xpath. If you were trying to find a div by partial id, for example:
//div[contains(#id, 'a:2:3')]
You don't say what language you are using, but in python, this would be used as follows:
driver.find_element_by_xpath("//div[contains(#id, 'a:2:3')]")
and in Java:
driver.findElement(By.xpath("//div[contains(#id, 'a:2:3')]"))
Assuming you are using Java
WebElement el = driver.findElement(By.cssSelector("td[id*='a:2:3']"));
The above code gets the element of TD which starts with a:2:3 as we use * in the css Selector.
XPath is more powerful and can be sometimes difficult to understand. CSS Selector is easy.

How to find all WebElements on page by presence of id?

I know I can find element by id:
driver.findElement(By.xpath("//*[#id='some_id']"));
And I can find all elements with this id, but I want to find all elements with an id attribute. I'm looking for something like:
driver.findElements(By.xpath("//*[#id]"));
// or
driver.findElements(By.xpath("//*[#id='*']"));
// or
driver.findElements(By.xpath("//*[contains(#id)]"));
I'm using Java. Thanks!
UPD: The only solution for me is to get all elements by "//*", go through them and get their id attributes. Is there a way to get all attributes at once, something like "//*#A" from Java?
Attribute A of element where A contains 't'
//E[contains(#A,'t')]/#A
or
//E[contains(#A,'t')]#A
Attribute A of any element
//*/#A
or //*#A
so I use java too. So if you need to find all elements with id attribute you can simply use
driver.findElements(By.xpath(//*#id))
Hope this helps you.
ALso look here. Nice manual for xpath and css selectors
have you tried "//*[contains(#id, '')]"