Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Selenium: How to click an element containing text and which parent is button

I have two buttons on a page containing "Einloggen" text and I need to click the "Einloggen" button.

The first element with "Probleme beim Einloggen?" is a link with span.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

and the second, which I need is a button with a span.

enter image description here

CSS classes are not stable, I cannot use them.

I have tried:

self.log_in_link = WebDriverWait(self.driver, self.delay).until(
            EC.presence_of_element_located((By.XPATH, "//button/following-sibling::span[contains(text(), 'Einloggen')]")))

But it can’t find it

If I try so

self.log_in_link = WebDriverWait(self.driver, self.delay).until(
            EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Einloggen')]")))

The driver clicks the wrong element ("Probleme beim Einloggen?")

>Solution :

From the picture you presented I see the parent element here is not a button rather div with buttonstyle class.
Please try this XPath

//div[contains(@class,'buttonstyle')]//span[contains(text(),'Einloggen')]

I would also advice using visibility_of_element_located expected condition instead of presence_of_element_located to wait for more mature element state, when it becomes visible, not just presented (but may be still not completely rendered).
So your code could be

WebDriverWait(self.driver, self.delay).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'buttonstyle')]//span[contains(text(),'Einloggen')]")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading