I'm still trying to make a bot with python and selenium. I'm trying to click on the element with the highest value. The highest value is stored in the element
variable. I tried this script, but selenium doesn't manage to locate it...
element = driver.find_element_by_xpath('//p[#value="' + value_to_locate + '"]')
driver.execute_script("arguments[0].scrollIntoView;", element)
element.click()
Here is the HTML :
<p data-v-859a1d26="" class="ml-2">632 Bells</p>
Do you have any idea?
From W3Schools:
The value attribute specifies the value of an element.
If you have multiple paragraphs <p> with different values, what you really need is getting all the paragraphs' values, sorting them, and clicking the max.
Get multiple elements using driver.find_elements_by_xpath (note the plural - elements) and then use Python to execute the sorting logic.
element.text will get you the text content of the element.
Maybe your XPath gets screwed up when you are trying to '//p[#value="' + value_to_locate + '"]' try something like "//p[#value='{}']".format(value_to_locate)
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()
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']"
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
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
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]")