xpath selenium locating next sibling – any element

Question:

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]')

enter image description here

enter image description here

Asked By: xlmaster

||

Answers:

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:

  1. //span[contains(text(),"by")] matches more than 16 relevant elements.
  2. /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.
Answered By: Prophet