3

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!

1 Answers1

1

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352