The URL is enter link description here
Is there any way in selenium XPath to get value the element just the element which following sibling of located element
//span[contains(text(),"by")]//following-sibling::a
in this code I want to take the next element because sometimes it is not an anchor tag but span
For getting values of Author I am using the way, searching for "by" word element then
x_authors=driver.find_elements(By.XPATH,'//span[contains(text(),"by")]//following-sibling::a')
x_authors_a=driver.find_elements(By.XPATH,'//span[contains(text(),"by")]//following-sibling::span[1]')
The following XPath will give exactly what you need:
"//span[text()='by ']/following-sibling::*[1]"
There are several points to be improved in your initial XPath:
//span[contains(text(),"by")] matches more than 16 relevant elements.
/following-sibling::* selects All the following siblings, of any tag name. But this selects all the following siblings, not only adjacent following siblings. To make this precise [1] index added.
Related
Hi im trying to navigate from page 1 to page 5 (the element can be in any of the pages)and find and click on a specific element using python selenium.
The following is the element from the page:
<span _ngcontent-mtx-c123"" class"ng-star-inserted">ABC Company</span>
i tried by using : driver.find_element_by_name("ABC Company").click() but this doesnt work.
Another Way i tried:
1. element_path="//span[contains(text(),'ABC Company')]"
2. while True:
3. if(driver.find_elements_by_xpath(element_xpath)): driver.find_element_by_xpath(element_xpath).click()
4. else: driver.find_element_by_xpath("xpath to goto next page").click()
I need the code to find element from the next pages until its found and then click it.
is there any other way to do this???
Thanks in Advance
First, you need to check if the element is present and only if it does - click it. Otherwise you will get exception while trying clicking non-existing element.
driver.find_elements returns a list of web elements matching the passed locator. So if there are such elements it will return non-empty list interpreted as True by Python. Otherwise empty list is returned interpreted as False.
As about the locator for the element you are looking for: you can locate the element according to the text it contains. It can be done with XPath.
As following:
element_xpath = "//span[contains(text(),'ABC Company')]"
if(driver.find_elements_by_xpath(element_xpath)):
driver.find_element_by_xpath(element_xpath).click()
If you need to update the XPath locator dynamically you can pass the text as parameter. Let's say you have a list of texts, you can iterate on them as following:
for txt in texts:
element_xpath = "//span[contains(text(),'{}')]".format(txt)
if(driver.find_elements_by_xpath(element_xpath)):
driver.find_element_by_xpath(element_xpath).click()
I need to retrieve the xpath of element on a page but all I know about the element is that it will contain a certain string let's say "required text" and that it will sit in a div. I would like to search the page for this string and return the xpath to this div.
I have tried something like:
${findXPath} = Get Element Attribute //*[contains(text(),'required text')] xpath
but this returns nothing.
XPath isn't an WebElement's attribute. xpath is a element Locating Strategy.
So Selenium won't be able to return the xpath
On this website https://classicdb.ch/?quest=788
I tried:
driver.find_element_by_xpath(
"//div[contains(text(), 'Start')]").text
It finds the element and it returns
'Start: Kaltunk'
However when I try to find the element that contains "End" it doesn't finds anything.
driver.find_element_by_xpath(
"//div[contains(text(), 'End')]").text
Why is this?
Thank you.
Try with the below xpath.
//table//div[contains(.,'End:')]
Screenshot:
Explanation: Edit 1
First of all let's see how many text() nodes are present under the target div.
So the div have 3 text nodes.
Let me elaborate the original xpath used by OP.
//div[contains(text(), 'End')]
^div present anywhere in the document
^which contains
^the **first** text() node with string value as `End`
When contains() is given as its first argument (in div[argument]), it takes the string value of the first node, but End appears in the second text node, not the first. That's the reason why the xpath did not worked.
We have 2 options to handle this.
1) using text() as the first argument - By that way it will get all text nodes under current context and then use contains() as a condition to check for the text() value that will match any text() node whose value contains End as shown below.
//div[text()[contains(., 'End')]]
^div present any where in the document
^which have text() node
^ that contains 'End`
Check the below screenshot:
By this time, you would got a question then why the first xpath (//div[contains(text(), 'Start')]) used by OP worked?
If you look at the text() nodes associated in the div, Start text is present in the 1st text() node itself, that's the reason why he was able to use that xpath.
2) Using . to check in current node context In simple terms when you say . it will check in the entire current element context for the End.
//div[contains(.,'End')]
If you don't limit the scopt to //table (at the beginning of the xpath) you will get 5 divs as the ancestor divs of the original div which have this text also be matched with the xpath. So limit the scope to check with in the table like
`//table//div[contains(.,'End')]
So I have this site and I'm trying to obtain the location and size of an element based on this xpath "//div[#class='titlu']"
How you can see that is visible and has nothing special.
Now the problem I've faced is that when I'm doing the search for xpath like this
e = self.driver.find_element_by_xpath(xpath) the location and size
of e are both 0
Also, for some reason, if I'm trying to get the text like this:
e.text is going to show me an empty string, and I need to get the actual text in this way e.get_attribute("textContain")
So do you have any idea how can I get the location and size of this element?
There are two elements matching this xpath. driver.find_element_by_xpath returns the first one while you are looking for the second one. Use the ancestor <div> with id attribute for unique xpath
"//div[#id='content-detalii']//div[#class='titlu']"
I want to get the inner html of an element (with get_attribute('innerHTML')) but it doesnt have and id or class and there are multiple elements with the same tag name
test1=driver.find_elements_by_tag_name("td")
This gets the whole list of elements with the same tag name but this doesnt work because get_attribute doesnt work with multiple elements
test2=driver.find_element_by_tag_name("td")
this works but gets the very first td elements but i want the second td element
How do i do this correctly?
you can use xpath like below to get the second td of every row in a table.
driver.find_elements_by_xpath("//table/tr/td[2]")
modify xpath to go to your required table if you need it from a particular table.
As per your question the following line of code is returning you the very first td element :
test2=driver.find_element_by_tag_name("td")
To retrieve the text within the second td element you can use either of the following lines of code :
xpath :
test2 = driver.find_element_by_xpath("//table//tr//following::td[2]").get_attribute("innerHTML")
css_selector :
test2 = driver.find_element_by_css_selector("//table > tr > td:nth-last-child(2)").get_attribute("innerHTML")
Note : The last part of the xpath and the css_selector will definitely identify the second <td> element but you may have to require to adjust the initial part as per your HTML DOM