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

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.

Related

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

Selenium unable to find Button

I am trying to click on the "Next Page"-Button on the Web of Science Search-Site to iterate through all pages.
Here is a screenshot of the HTML of the page (highlighted is the button)
This is my code to find the button:
driver.find_element_by_class_name('mat-focus-indicator mat-icon-button mat-button-base').click()
But I receive this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mat-focus-indicator mat-icon-button mat-button-base"}
I have tried so many ways of identifying the button (find_by_id, find_by_name, find_by_link_text) but nothing works.
What am I doing wrong?
Thank you in advance
Maybe try with Query Selector like:
driver.find_element_by_css_selector('button[data-ta="next-page-button"]')
You can always try selectors on elemnts panel (like on screenshort) and type your selector in field "Find by string, css, or xpath"
Class name do not have support for spaces, remove spaces and put . to make it css selector :
driver.find_element_by_css_selector('button.mat-focus-indicator.mat-icon-button.mat-button-base').click()
also try to put some wait before this to make it more consistent.

Find element by label using Selenium

I got a button named "Photos" that I want to find through Selenium:
<label class="Label" id="item--label">Photos</label>
I tried to find it through XPath but I got the code below (which repeats in more buttons in the page):
//*[#id="item--label"]
Is there a way to locate the item by label?
I have tried:
driver.find_element_by_tag_name('Photos')
But no success.
If anyone could help, I would be glad!
Thanks!
The most complete XPath expression for this element is
//label[#class="label" and(#id = "item--label") and(text()="Photos")]
So to find it with Selenium you can use this command:
button = driver.find_element_by_xpath('//label[#class="label" and(#id = "item--label") and(text()="Photos")]')
But you definitely do not need to mention all these attributes
driver.find_element_by_xpath(".//label[text()='Photos']")
Try this xpath:
//*[#id="item--label" and text()="Photos"]

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

Selenium Python -- Message: element not visible

I am trying to click on a href link.
I managed to get the link by
element = browser.find_element_by_xpath("//a[contains(#href,'main.cfm')]")
But when I was usingelement.click(), it showed
Message: element not visible
I am not sure why. Could it be the "==$0" in the picture? When I moved the mouse on to the text it says "Use $0 in the console to refer to this element".
Answer of #Shoaib Akhtar is right, You can also use this xpaths
//span[contains(text(),'Latest Projects')]
//span[contains(#class,'uppercase')][contains(text(),'Latest Projects')]
Try this xpath
//a[span[text()[contains(., 'Latest Projects')]]]
this is to select parent link of 'span' element which has substring text 'Latest Projects'.

Categories