Find div id of an innerHTML text in Python Selenium - python

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()

Related

unable to interact with this dynamic drop down using python selenium

hi team,
I am trying to access a dynamic drop down with div as tag but I an able find it but not interact with it
as it changes its style type as shown below.
<div style ="display :none;"></div>"
to
<div style ="display :block;"></div>"
I am unable to click on this, please have a look into the screenshot for detail.
Info you have to click on the element to access this dynamic dropdown,
div is not clickable object in HTML. If it has assigned some JavaScript code to display when you click it then you may need also JavaScript to click it
driver.execute_script("arguments[0].click()", item)
and the same way you can change style
driver.execute_script("arguments[0].style.display = 'block';", item)
In this minimal working example I remove all img on this page.
from selenium import webdriver
url = 'https://stackoverflow.com/questions/65931008/unable-to-interact-with-this-dynamic-drop-down-using-python-selenium'
driver = webdriver.Firefox()
driver.get(url)
all_items = driver.find_elements_by_xpath('//img')
for item in all_items:
print(item.text)
#driver.execute_script("arguments[0].click()", item)
driver.execute_script("arguments[0].style.display = 'none';", item)
Solution -> actually the element we are looking here is masked element which means actual Id of this element is diff so by chance I am able to find it( for this you have go through HTML code row by row and find it, quiet a static way to do but that's how I did it) and place it in the code.
please do comment if you know more efficient way to work around masked elements.
Regards,
Anubhav

When I print the id it doesn't match the id in "inspect element"

I have a webpage with the following:
<span class="plugin_pagetree_children_span plugin_pagetree_current" id="childrenspan173273808-0"> Shift Turnover </span>
and I can successfully find it by link text using st = driver.find_element_by_link_text('Shift Turnover')
but when I print the id using print('-',st.id)
The id prints out as 63cd644e-495b-4985-8f9e-7ea067a2b6f1 instead of childrenspan173273808-0.
I've also attempted to get_attribute and get_property but those aren't working either. Any hints/tips/suggestions welcomed.
Thanks in advance.
The issue I'm seeing here is that you are trying to get the ID childrenspan173273808-0, but your selector driver.find_element_by_link_text('Shift Turnover') is locating the a element, which has no ID. That is why get_attribute is not working for you. You actually want to find the span element, which contains your desired ID.
You can use this to get the ID childrenspan173273808-0:
st = driver.find_element_by_xpath("//span[a[text()='Shift Turnover']]") # locate the span
id = st.get_attribute("id") # get its ID and print
print(id)
This XPath locates the span element that appears outside of the a element with text Shift Turnover. We query on the span which contains a element with Shift Turnover text, then call get_attribute on the span element, to retrieve your desired childrenspan173273808-0 ID.
Lastly -- the ID 63cd644e-495b-4985-8f9e-7ea067a2b6f1 that was printing out in your example was not the WebElement ID attribute, but rather, "the server-assigned opaque ID for the underlying DOM element". This is detailed in the Selenium docs on WebElement.
You are trying to retrieve value of web element. You need to use get_attribute method which is declared inside the Web element interface. Basically this method will return the value of the specified attribute in the string format.
Solution 1:
locator= driver.find_element_by_xpath("//span[#id='childrenspan173273808']//a").get_attribute("id")
print(locator)
Solution 2:
locator=driver.find_element_by_xpath("//span[#class='plugin_pagetree_children_span plugin_pagetree_current']//a[1]").get_attribute("id")
print(locator)

selenium following element 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()

Python, selenium Can't hook following-sibling element as xpath

I have following html code:
I need to hook the "edit" icon for particular element which has title "test".
I tried to do
//*[contains(text(), 'test')]/following-sibling::div/span[#title='Edit']
But it doesn't work.
I can't identify this element by just title = Edit, because there are a lot of elements which will have this button edit. The only unique this is first title "test" and following sibling "Edit".
Any ideas? Would appreciate any help.Thank you
You can select the div with title="test", then get the span with title="Edit" like this:
xpath("//div[#title='test']/following-sibling::div/span[#title='Edit']")
or directly select the div with span title='Edit':
xpath("//div/span[#title='Edit']")
Was able to locate this element with following path:
//*[contains(., 'test')]/following-sibling::div/span[#title='Edit']
Thank you everyone for your help.

Locate and Copy a Text from an HTML Tag

I have the following HTML code:
<td class="label">Name:</td>
<td colspan="3">COHEN </td>
I would like to retrieve the text from this HTML code==="COHEN".
I am working with Python Selenium and I need to put into a note this text but have no clue how can I select the text.
Thank you in advance,
Cohen
So in selenium there are webElements. WebElements have the attribute text, which is the visible text inside the element.
So all you need to to is find the WebElement and then get the attribute from it. If you are using xpaths it would look like:
element = driver.find_element_by_xpath("//td[#colspan='3']")
visibleText = element.text
Of course I imagine that you might have an issue with finding the xpath like this because I imagine that you will have more then 1 td tag, with colspans that are 3. But once you find the right element, getting the text from it is simple enough.

Categories