Python Selenium: Can't find element by link text - python

I am trying to select "seltarget" from a list of other options.
HTML:
<div class="filter-item" data-reactid=".3.1.0.0.1.0.0.3.0.0.0.3">seltarget</div>
When exported to a python file it recommends that I locate the element by xpath:
driver.find_element_by_xpath("//div[#id='quickswitcher']/div/nav/ul/li/div/div/menu/div[2]/div/div/div/div[5]").click()
This is working for the time being because "seltarget" just happens to be 5th in the list. The above line will select whichever option is 5th in the list. I want to be more specific and select the element "seltarget"

Locating by link_text works only with <a> tags. To locate this element you can use the class if it unique
driver.find_element_by_class_name('filter-item').click()
Or the data-reactid attribute
driver.find_element_by_css_selector('[data-reactid*="3.1.0.0.1.0.0.3.0.0.0.3"]').click()
To locate by the text you can also use css_selector
driver.find_element_by_css_selector('div:contains("seltarget")').click()
Or xpath
driver.find_element_by_xpath('//div[contains(text(), "seltarget")]').click()

If you want to find the div by its text, you can use the following.
driver.find_element_by_xpath("//div[text()='seltarget']")

Related

xpath selenium locating next sibling - any element

The URL is enter link description here
Is there any way in selenium XPath to get value the element just the element which following sibling of located element
//span[contains(text(),"by")]//following-sibling::a
in this code I want to take the next element because sometimes it is not an anchor tag but span
For getting values of Author I am using the way, searching for "by" word element then
x_authors=driver.find_elements(By.XPATH,'//span[contains(text(),"by")]//following-sibling::a')
x_authors_a=driver.find_elements(By.XPATH,'//span[contains(text(),"by")]//following-sibling::span[1]')
The following XPath will give exactly what you need:
"//span[text()='by ']/following-sibling::*[1]"
There are several points to be improved in your initial XPath:
//span[contains(text(),"by")] matches more than 16 relevant elements.
/following-sibling::* selects All the following siblings, of any tag name. But this selects all the following siblings, not only adjacent following siblings. To make this precise [1] index added.

How to use mutiple attributes (including a partial string match) with find_elements in Selenium for Python

Currently I have this line of code which correctly selects this type of object on the webpage I'm trying to manipulate with Selenium:
pointsObj = driver.find_elements(By.CLASS_NAME,'treeImg')
What I need to do is add in a partial string match condition as well which looks in the section "CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)" in the line below.
<span class="treeImg v65point" style="cursor:pointer;">CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)</span>
I found online there's the ChainedBy option but I can't think of how to reference that text in the span. Do I need to use XPath? I tried that for a second but I couldn't think of how to parse it.
Refering both the CLASS_NAME and the innerText you can use either of the following locator strategies:
xpath using the classname treeImg and partial innerText:
pointsObj = driver.find_elements(By.XPATH,"//span[contains(#class, 'treeImg') and contains(., 'AHU-01_ahu_ChilledWtrVlvOutVolts')]")
xpath using all the classnames and entire innerText:
pointsObj = driver.find_elements(By.XPATH,"//span[#class='treeImg v65point' and text()='CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)']")

Can't find element that exists using Python Selenium IE

I'm trying to type text in a text box
<input name="pesquisarUtente.numeroUtente" class="number numero-utente" id="pesquisarUtente.numeroUtente" type="text" maxlength="9" value="">
I've tried the following
driver.find_element_by_name("pesquisarUtente.numeroUtente").send_keys('123123123')
driver.find_element_by_id("pesquisarUtente.numeroUtente").send_keys('123123123')
I always get the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == [id="pesquisarUtente.numeroUtente"]
Try using Xpaths or Css Selectors.
This error commonly occurs when there is more than one element with the same id so selenium is expecting find_elementS_by_id.
Xpaths and Selectors are unique to each element so they work better when trying to find a specific element.
example using xpaths:
driver.find_element_by_xpath("ENTER XPATH HERE").send_keys('123123123')
To get the Xpath of an element you simply 1: hover over the element in the developer mode 2:right click on it 3:hover over the copy option 4:move down to copy Xpath and click
Css Selectors work the same way. (driver.find_element_by_css_selector("SELECTOR HERE"))
And you can see the option to copy one 3 above the option to copy the Xpath

Find xpath of an element from text contained within a div [Robot framework]

I need to retrieve the xpath of element on a page but all I know about the element is that it will contain a certain string let's say "required text" and that it will sit in a div. I would like to search the page for this string and return the xpath to this div.
I have tried something like:
${findXPath} = Get Element Attribute //*[contains(text(),'required text')] xpath
but this returns nothing.
XPath isn't an WebElement's attribute. xpath is a element Locating Strategy.
So Selenium won't be able to return the xpath

How to select one element with common class names in Selenium Webdriver using Python?

I'm new to Selenium Webdriver using Python.
I want to select the tag which contains 'Stats'. Tried multiple ways but failed as both the tags don't have any Id and have same class names as well.
Please help me with the codes to select the tag which contains 'Stats' using Selenium webdriver with python.
List of few Trials, Even if the result is found. I'm unable to click it and the error message is Lists cannot be clicked.
driver.find_element_by_class_name("css173kae7").click()
driver.find_elements_by_link_text("stats/dashboard").click()
driver.find_elements_by_xpath("//*[contains(text(), 'Stats')]")
I've attached the image of the Inspect element of the code, please have a look at it.
(Updated) Image
Inspect element of the "Anchor Tags" from which one needs to be selected
Use this code:
driver.find_elements_by_xpath("//*[contains(text(), 'stats')]")
if you are looking for "stats" in href attribute use below code:
driver.find_elements_by_xpath("//*[contains(#href, 'stats')]")
Keep in mind, contains is case-sensitive.
If you want to do case-insensitive search, See here.
Based on the image of the HTML you shared, I would try this XPATH locator to select that element:
driver.find_element_by_xpath(".//a[#class='css-1qdedno' and #href='/stats/dashboard']")
try this xpath
//nav//a[contains(#href,'/stats/dashboard')]
You need to select the desired element by its index value after you get all the elements in a list:
elements= driver.find_elements_by_xpath("//*[contains(text(), 'stats')]")
Then identify and access the element by its index and call the click() function:
For example if the element is at elements[0], then
elements[0].click()

Categories

Resources