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()
Related
I'm automating the process of filling some forms online. The problem is that there are many individual elements whose children have basically the same ID of the stuff I want to find and fill. So my idea was to first find the parent I needed using Selenium and then go from there.
for range in cards:
cardID = driver.find_element(By.XPATH, "//a[contains(text(),'{cardis}/')]/ancestor::tr".format(cardis=allCards_NUMBER_List[range]))
cardREG_PRICE = cardID.find_element(By.XPATH, "input[contains(id(), 'txt_preco_')]")
But when I run this it only said that it can't find cardREG_PRICE. The ID name is correct, and from what I've read the XPATH structure should work. How can I fix this?
Your xpath is incorrect.That's why it is failing.
Instead of this
cardREG_PRICE = cardID.find_element(By.XPATH, "input[contains(id(), 'txt_preco_')]")
if should be like.
cardREG_PRICE = cardID.find_element(By.XPATH, ".//input[contains(#id, 'txt_preco_')]")
first thing id is an attribute and should pass with #, second thing // denote the node, Third thing . means intermediate child of the parent.
I need to find a location on the webpage by the visible text. Then I need to copy the value that is adjacent to it. It makes more sense when you see the screenshot.
So the idea is to find the text "Net PPW" and then grab the value, which for this example is "1.56".
I assume this is possible since they are in the same section of the webpage but I have no clue how to do this or where to start.
One of the simpliest solution is mannually check every p element text on page to get next argument with p tag in result(cause they always stay together).
elements = driver.find_elements(By.TAG_NAME, 'p')
for x in range(len(elements)):
if elements[x].text == "Net PPW"
print(elements[x+1].text)
With use of XPath you can locate the parent element based on it's parent and then locate the parent element, as following:
driver.find_element(By.XPATH, "//div[contains(#class,'justify-content-between')][contains(.,'Net PPW')]//p[contains(#class,'MuiTypography-body1')]").text
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 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'?