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
Related
I have the custom attribute called upgrade-test="secondary-pull mktg-data-content in the following code snippet:
<section class="dvd-pull tech-pull-- secondary-pull--anonymous tech-pull--digital secondary-pull--dvd-ping tech-pull--minimise" upgrade-test="secondary-pull mktg-data-content" data-js="primary-pull" style="--primary-direct-d_user-bottom-pos:-290px;">
I am able to identify my element successfully by doing the following:
element = driver.find_element(By.XPATH, "//section[contains(#upgrade-test, 'mktg-data-content')]")
This mktg-data-content gets changed every time a user goes to a different page for example it could be sales-data-content for the sales page etc.
What I am after is to find a way to retrieve this dynamic text of this custom attribute and pass it to my variable. Any help would really be appreciated. Thanks
You need to fetch the attribute value using element.get_attribute("upgrade-test") and then need to do string manipulation.
elementval = driver.find_element(By.XPATH, "//section[contains(#upgrade-test, 'secondary-pull')]").get_attribute("upgrade-test")
print(elementval.split(" ")[-1])
Note:- splitted with space,which returns zero based list, index -1 means the last value of the list,
since you have two elements in the list you can use this as well
print(elementval.split(" ")[1])
You need to find a unique locator for that element.
Without seeing that page we can only guess, so I can guess tech-pull--digital and secondary-pull--dvd-ping class names are making a unique combination.
If so you can use the following code:
attribute_val = driver.find_element(By.CSS_SELECTOR, "section.tech-pull--digital.secondary-pull--dvd-ping").get_attribute("upgrade-test")
print(attribute_val.split(" ")[-1])
The first line here locates the element and retrieves the desired attribute value while the second line isolates the first part of the desired attribute value as explained by KunduK
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']"
I have this piece of Html:
<tr class="selectable" onclick="PesquisarProntuarioView.EditarProntuario('108077098085')">
I would like to get what is inside onclick, i was looking for a command which would give me:
"PesquisarProntuarioView.EditarProntuario('01048108077098085')"
I already have tried several commands like this one:
element=driver.find_element_by_xpath("//a/tr/#onlick=PesquisarProntuarioView.EditarProntuario(*)")
driver.get(element)
However still no clue, Could someone please help me with the respective command in Selenium/Python?
Locate the element and use get_attribute() method:
element = driver.find_element_by_xpath("//a/tr")
print(element.get_attribute("onclick"))
If there are multiple elements you need to extract onclick from, use find_elements_* and call get_attribute() for every element found:
for element in driver.find_elements_by_xpath("//a/tr"):
print(element.get_attribute("onclick"))
I would slightly change the above solution to only select relevant elements in path //a/tr by providing part of the onclick attribute, like this:
onclickValue = driver.find_element_by_xpath("//a/tr[contains(#onlick,'PesquisarProntuarioView.EditarProntuario')]").get_attribute("onclick")
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]")