Python selenium get location of an element - python

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

Related

Search for a specific element in multiple pages and click using Python Selenium

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()

XPATH not finding the text that is contained on the web - using contains(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')]

Read a dynamic value using Selenium

Using Selenium I would like to the value of a element in a HTML. Like the following picture shows. The value "1841" is what I would like to extract. I can find the element for the next element by:
e = b.find_element_by_xpath("//*[contains(text(), 'value_to_alert')]")
Now, the problem is I need to get the element before this element... Note that I cannot use the class name because there are a bunch of "KpiValue"s. Is there a way to do that?
Thanks.
To retrieve the text 1841 from the previous div you can use the following line of code :
text = b.find_element_by_xpath("//div[contains(.,'value_to_alert')]//preceding::div[1]").text

Getting ID of Selenium WebElement in Python?

If I have the following HTML <a id="id" class="class" href="href">Element Text</a> how would I get "id" to be returned? My current procedure is:
print("Attribute: " + element.get_attribute('id'))
print("Property: " + element.get_property('id'))
print("Class: " + element.get_attribute('class'))
But all of those return empty strings. I am, however, able to get the text by using element.text
EDIT: Here's a more in depth explanation
I'm looking for an element but that element's ID varies. There is, however, an element linked to the element I want that I can find using it's xpath and comparing it's text to a specific text that I know beforehand. The ID of that element is something in the form of someID_XX. By taking the XX and appending it to another fixed string, I can then search for the element that I actually want. My issue is that once I get the second element (not the one I want directly, but the one that can lead me to the one I want) I can't seem to get it's ID attribute even though it seems to have one in the html. My question is, how do I get the id attribute?
For me is working with
element.get_attribute('id')
At the beginning didn't work because I selected incorrectly a hidden javascript element without id.
But if the error persist on your case, you can try this:
element.get_attribute('outerHTML')
get the DOM for the element, and then parse the html to extract the id

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