Reputation: 155
On the webpage I am wroking, I might encounter two types of input fields (depending on the situation) - it will be either select or input, with the following example xPath:
Case Select
/html/body/div[1]/section/div/div/div/div/form/div[1]/div/div[4]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr[20]/td[3]/div/select
Case Input
/html/body/div[1]/section/div/div/div/div/form/div[1]/div/div[4]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr[19]/td[3]/div/input
because I don't know what will be the field type, I am going to use following sytnax:
/html/body/div[1]/section/div/div/div/div/form/div[1]/div/div[4]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr[20]/td[3]/div/*
As I understand, when I use selenium driver.find_element_by_xpath, with "*" example of the path, code will return me first element which matches the criteria?
How can I use something like "find next"?
How can I distinguish between Input and Select field?
I tried the following:
Upvotes: 0
Views: 499
Reputation: 33371
driver.find_element_by_xpath
you can use driver.find_elements_by_xpath
.elements = driver.find_elements_by_xpath("your_xpath")
elements[0]
will give you the first element matching your_xpath
while the second element matching the your_xpath
locator will be elements[1]
2) to get already selected element's tag name you can use obj.tag_name
method.
To select the desired element explicitly you can mention it's tag name in your XPath locator so the selected element WILL be of desired type.
You can use something like //*parent_elements//select
to get the Select element and correspondingly //*parent_elements//input
to get the input element.
As for the get_attribute
method - cruisepandey already referred you to the documentation. I just want to clarify, that attributes are not including tag name. So, tag name is not an attribute property of the element.
Upvotes: 1
Reputation: 29382
First question :
As I understand, when I use selenium driver.find_element_by_xpath
, with "*" example of the path, code will return me first element which matches the criteria?
How can I use something like "find next"?
Ans : yes it will return the first element, in case you want all of them, switch to find_elements
. for this How can I use something like "find next"?
- what do you mean by next ? - I would assume that as a next sibling, if that is the case then you can make use of ::followin-sibling
2nd Question :
How can I distinguish between Input and Select field?
pretty simple with the xpath :-
//input
to select all input, you can have it like //input[attribute_name='attribute_value']
to distinguish b/w input fields.
and
//select
and can use it more constructive like :
//select[attribute_name='attribute_value']
Now,
I would expect return like "Select" or "Input".
you can use tag_name
, like below :-
obj.tag_name
print this and you should get the tag name appropriate.
Where can I find documentation related to what can I write inside "get_attribute"?
get_attribute
is to fetch attribute value, for the specified web element -> attribute name.
This is what officials says :
get_attribute(name) Gets the given attribute or property of the element.
This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.
Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.
Find the official document here for get_attribute
Upvotes: 1