selenium following element python - python

I'm using selenium with python.
I have some element that is a checkbox, and I want to click on it.
My problem is with getting that element, I have only the text
In my case <td>xxxxx</td> and I want to get the element above it (the previous element, he is not is father, they are only adjacent)
I tried this:
driver.find_element_by_xpath("//input[#type='checkbox']/following::td[text()='xxxxx']").click()
but it didn't work.

You can navigate from td with following xpath
driver.find_element_by_xpath("//td[text()='xxxxx']/preceding-sibling::td[1]/input[#type='checkbox']").click()
or you can try with parent tag and navigate nth td which has input tag like
driver.find_element_by_xpath("//td[text()='xxxxx']/parent::tr/td[1]/input[#type='checkbox']").click()

Related

Get Xpath from already known element

Friends, I'm doing a web scraping. I'm looking for an element that has the XPath changed every time it's searched. For that I'm looking for a way to get the correct xpath.
For this I will need to get the Xpath of an element that I can already locate, it is not the element that I need to locate. But with his Xpath I can find the Xpath of the desired element. so i'm searching:
element = self.chrome.find_element_by_xpath("//div[text()='Apelação']")
With that I need to get your XPath which is:
//*[#id="consultarProcessoForm:dtProcessos_data"]/tr[2]/td[4]/div
How can I do this?
I tried using this; but it returned nothing
print(element.get_attribute("id"))
This is the code of the page:
enter image description here

Find div id of an innerHTML text in Python Selenium

Sorry if this has been asked, but I am struggling for some time to find a solution for my problem and I cannot find any answer. I am doing some tests in Selenium and trying to automate some actions on a platform.
The trick is that I want some elements to be clicked from drop-down lists. All elements looks like the code below:
<div class="tag" id="ATagBox02#QUK#4761"><div class="ellipsis" style="padding-right: 2px; float: left;">EXAMPLE</div></div>
If I run a code such as below or something similar:
driver.find_elements_by_xpath("//*[contains(text(), '%s')]" % value).click()
where "value" is the element I want to be selected, I get that the element is not clickable.
What I want to do to solve this problem is to find the div id for the text of the element, which is clickable. Then store the div id in a variable and get it clicked using a line such as the following:
driver.find_element_by_id("%s" % ID).click()
So that Python will understand:
driver.find_element_by_id("ATagBox02#QUK#4761").click()
What I don't know is how to search for div id of a specific innerHTML text. How I can tell Python to establish that "ATagBox02#QUK#4761" is the id for the element "EXAMPLE"?
Thank you in advance!
To locate a WebElement through it's text and then to find the id of the parent DIV and click an element through the id you can use the following solution:
value = 'EXAMPLE'
element_id = driver.find_element_by_xpath("//*[contains(text(), '%s')]//ancestor::div[1]" % value).get_attribute("id")
driver.find_element_by_id(f"{element_id}").click()

How to find an Element by index in selenium Webdriver for python

This is HTML code of that page
From there I want to access the 2nd element by using class name "maxbutton-1" as it has 3 same buttons and I can't use xpath or any constant selector so want to use the indexing with class and can't find anything to do that in python particular.
Also tried the method used in java to do same thing but it didn't worked.
Link of that same page
just trying to automate the movie downloading process for any movie.
Thank you.
To click on first, second or third button, try to change number of element:
el1 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[1]")
el2 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[2]")
el3 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[3]")
then you can extract element href/link attribute like that:
link = el.get_attribute('href')
or click it like that:
el.click()

Click on li element searching specific displayed text

Using python 3 and chrome driver. I'm trying to click on my desired element searching for the text displayed on this page . For example, in case of "BEBES" I'm using:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(text(), "BEBES")]'))).click()
but nothing happens. Just throws the time out exception. What's my error?
Your xPath is not correct. Use this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[contains(text(), "Bebes")]'))).click()
Note: upper/lowercase makes difference
and
This post suggests using the following as text() returns a node set:
//*[text()[contains(.,'BEBES')]]
XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Using selenium to get access class info on website

I am using the following code using Python 3.6 and selenium:
element = driver.find_element_by_class_name("first_result_price")
print(element)
on the website it is like this
`website: span class="first_result_price">712
however if I print element I get a completely different number?
Any suggestions?
many thanks!!
"element" is a type of object called WebElement that Selenium adds. If you want to find the text inside that element, you have to say
element.text
Which should return what you're looking for, '712', albeit in string form.

Categories