Find Element by partial Xpath in Selenium - python

I am trying to define specific elements, the only way in which they differ is how the 'div' changes like below (the 1st element I want has div[1], second div[2] etc.
//*[#id="search-content-container"]/div/bls-search/div/div/div[2]/div[2]/div[4]/bls-connection/div/div[2]/div**[1]**/bls-connection-step/div/div[1]/div[1]/div/div[2]/span[1]
//*[#id="search-content-container"]/div/bls-search/div/div/div[2]/div[2]/div[4]/bls-connection/div/div[2]/div**[2]**/bls-connection-step/div/div/div[1]/div[1]/div[2]/span[1]
//*[#id="search-content-container"]/div/bls-search/div/div/div[2]/div[2]/div[4]/bls-connection/div/div[2]/div**[3]**/bls-connection-step/div/div/div[1]/div/div[2]/span[1]
How do I specify this in Selenium to allow me to find all 'i'?

Related

xpath selenium locating next sibling - any element

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.

Python selenium wait until an element located not by xpath?

I am scraping a webpage in multiple pages, pressing certain buttons will advance to the next page, but all the pages share exactly the same url, means most elements I have scraped aren't visible until certain buttons of the same type before them are clicked, thus trying to click them will raise the following error:
ElementNotInteractableException: Message: Element <div class="answer"> could not be scrolled into view
The elements are all of the same class, and they are all children of instances of another class, each of the parent classes have multiple instances of the child class.
To find the elements, I use two times .find_elements_by_class_name() method:
lists = []
for i in Firefox.find_elements_by_class_name('parent'):
lists.append(i.find_elements_by_class_name('child')
There is exactly one element in each of the sublists that needs to be clicked, the element is determined by its attribute and identified using list indexing, so I have absolutely no idea what its xpath is.
Each of the elements aren't visible until the preceding element is clicked, so wait for them is a must to avoid ElementNotInteractableException.
I am using the following syntax:
wait = WebDriverWait(Firefox, 3)
wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
And I need to find the xpath of an element located using the aforementioned methods, unfortunately Selenium doesn't support this natively.
But I have found a trick here:
In [1]: el
Out[1]: <Element span at 0x109187f50>
In [2]: el.getroottree().getpath(el)
Out[2]: '/html/body/div/table[2]/tbody/tr[1]/td[3]/table[2]/tbody/tr/td[1]/p[4]/span'
So I think, if I can build lxml tree from selenium page source then somehow convert selenium elements into lxml elements then it can be done, though I don't know exactly how...
What about something like this (maybe you need to use contains for class):
wait.until(EC.visibility_of_element_located((By.XPATH, f'//*[#class="parent"][{index}]//*[#class="child"][{subindex}]')))

Python - Find Xpath value then click button

I'd like to get some advise on how to find a value in certain xpath value, time in my case: 7:30pm.
Then in the same level click on an add button. (there are many other same button in the page with a different time value). Reference to attached picture, really appreciate any help.
Probably every web browser in DevTools has function to get XPATH on right click.
But you could try also find unique ID or CLASS (or other unique value - ie. style=...) for element. Or unique value for its parent or grandparent.
If there are many similar elements then you can get all of them and later use index - all_times[1] - to work only with one element.
It seems your element has unique headers="Times" which you could use in xpath.
You can also relative xpath (starting with dot .) and search partially - first find tr, next find .//td[#headers="Times"] in this tr and next find .//td[#headers="Avaliable"]/div/a in the same tr
EDIT:
If I couldn't find unique elements then I would get all tr because it is parent for Times and Avaliable and I can use relative xpath to search one Times and one Avaliable in single tr - and then use for-loop
all_tr = html.xpath("//tr")
for tr in all_tr:
# relative xpath in `tr` (using `.` at start)
header = tr.xpath('.//td[#headers="Times"')
if "7:30" in header[0].text:
# relative xpath in `tr` (using `.` at start)
avaliable = tr.xpath('.//td[#headers="Avaliable"]')
avaliable[0].click()

Python selenium get location of an element

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

Get element inside current element using xpath

I have multiple questions inside which there are more than 1 options.
After selecting the required question element as question_element
I am unable to get the first text box inside this element. I used
question_element.find_elements_by_xpath("//textarea")
but it gives me list of all the elements with tag textarea in the whole webpage. I tried
question_element.find_elements_by_xpath("/textarea")
question_element.find_elements_by_xpath("./textarea")
but they didn't give any results. How do I get the first element with tag name textarea inside the question_element
There are two variants that work for search within already found element (not within the whole page):
question_element.find_elements_by_xpath(".//textarea")
Try like this
question_element.find_elements_by_xpath("//textarea[position()=1]")

Categories

Resources