Selenium Unable to Locate Element, but it surely exists - python

EDIT FROM FUTURE, MAY BE YOUR SOLUTION: The page that I was trying to deal with opens on a new tab which I've missed to notice. My driver was trying to find the element on the very first tab, that is why it was unable to locate element.
--
--
--
I tried all solutions on Stackoverflow, however still got the same problem.
I am trying to select a specific option from a dropbox. It looks like this:
click to see the dropbox.
The HTML that belongs to this dropbox is as follows:
<td ct="GLC" lsdata="{1:'STANDARD'}" id="WD18" class="lsContainerCell lsGLCTopVAlign lsContainerCellVAlign--top urLayoutDefault--grid" valign="TOP" align="left" style="white-space:normal;"><span id="WD19-r" class="lsField lsField--standalone lsField--explicitwidth lsField--hasellipsis lsField--hashelp lsField--list lsField--focus lsField--interactiontarget" aria-live="assertive" style=""><input id="WD19" ct="CB" lsdata="{2:'WD1A',3:'10',4:'2021\x2f2022',27:'WD17'}" lsevents="{Select:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" type="text" autocomplete="off" tabindex="0" ti="0" class="lsField__input" readonly="" value="2021/2022" role="combobox" aria-haspopup="listbox" aria-readonly="false" aria-controls="WD1A" aria-expanded="false" aria-autocomplete="list" style="width:150px;" title=""><span id="WD19-btn" class="lsField__help" tabindex="-1" ti="-1" aria-hidden="true" role="button"></span></span></td>
Nothing I have tried did work because Selenium can not locate element. I've tried it with By.ID and By.XPATH. Last thing I have tried is this:
driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td/div/div[1]/span/span[1]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]")
But I still got the same error for the thousandth time:
Message: no such element: Unable to locate element
Thanks in advance.

Try this Xpath, avoid absolute path (click element, get xpath -_-)
driver.find_element(By.XPATH, "//td[#ct='GLC']")

Related

How to click a Vue/Vuetify card using selenium in python?

I am using selenium version 4.0.0, for reference.
I'm trying to click a Vuetify card element, which is acting as a button, but I running into an element not interactable: [object HTMLDivElement] has no size and location or just element not interactable errors. I have been able to solve similar problems with action chains in the past, but it doesn't seem to work with this.
This is the list element the button is contained within:
<li class="lu-li list-item" data-v-711d8d7a="" style="display: flex;">
<div class="addCard v-card v-card--link v-sheet theme--light" data-v-711d8d7a="" tabindex="0" onselectstart="return false;">
::before
<i class="v-icon notranslate btnAdd material-icons theme--light enableIcon" data-v-711d8d7a="" aria-hidden="true">
add
::after
</i>
</div>
</li>
The first things I tried was simply clicking on the element, and then clicking on the <i> element beneath it when that didn't work:
addQLButton = driver.find_element(By.CLASS_NAME, "addCard")
addQLButton.click()
addQLButton = driver.find_element(By.CLASS_NAME, "btnAdd")
addQLButton.click()
I have already tried using WebDriverWait on both the v-card <div> and <i> element to make sure they are available before being clicked, which it passes without any issues:
WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CLASS_NAME, "addCard"))
WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CLASS_NAME, "btnAdd"))
I have already tried using action chains to make sure the elements are visible (this has worked in the past with similar errors), but I still run into the same issue:
addQLButton = driver.find_element(By.CLASS_NAME, "addCard")
actions.move_to_element(addQLButton).perform()
driver.execute_script("arguments[0].click();", addQLButton)
addQLButton = driver.find_element(By.CLASS_NAME, "btnAdd")
actions.move_to_element(addQLButton).perform()
driver.execute_script("arguments[0].click();", addQLButton)
The elements are not in an iframe, and I am sure I have the correct window selected as I am still able to interact with its elements.
I am at a bit of a loss, any help would be much appreciated. I'm happy to answer any clarifying questions if I didn't explain the issue clearly enough.
I managed to get it working with some javascript:
js = "document.querySelector('.btnAdd').click();"
driver.execute_script(js)

Selenium and HTML

I have been using selenium to try and scrape some info from a database of company data. The database is private and it requires a password so, unfortunately, I cannot share the whole python code, nor the whole HTML code.
Here's the part of the HTML code required for my problem
<li>
<a id="reportsForm:j_idt320:3:j_idt325" href="#" class="ui-commandlink ui-widget nav-item " onclick="PrimeFaces.addSubmitParam('reportsForm',{'reportsForm:j_idt320:3:j_idt325':'reportsForm:j_idt320:3:j_idt325'}).submit('reportsForm','_blank');return false;" target="_blank">
<span id="reportsForm:j_idt320:3:l2" data-hasqtip="reportsForm:j_idt320:3:tooltipID">Avaliação de Risco Plus</span></a>
</li>
In this part of the code, I need to click on a linked text, and I have tried countless ways (including implicit waits, finding elements by class name, id, xpath and so on...) and I have realised that the only way to locate the element is through the following command:
afg = driver.find_element_by_link_text('Avaliação de Risco Plus')
However, when I try to click on it, I run into the following error message:
Message: element click intercepted: Element Avaliação de Risco Plus is not clickable at point (550, 745). Other element would receive the click: <div id="fundo">...</div>
I understand python is not letting me click on this what I defined as afg, because it is intercepted by the link on href = "#". But that is exactly the link I want to click!
How can I solve this?
Thanks for your help :)
If it is still in view point, you can try with below code :
element_to_be_clickable = driver.find_element_by_link_text('Avaliação de Risco Plus')
ActionChains(driver).move_to_element(element_to_be_clickable).click().perform()
or
ActionChains(driver).move_to_element(driver.find_element_by_link_text('Avaliação de Risco Plus')).click().perform()

Python & Selenium - Find element by label text

Im trying to locate and click an element (checkbox) from a big selection of checkboxes on a html site using python and selenium webdriver. HTML code looks like this:
HTML Code
<div class="checkbox-inline col-md-5 col-lg-3 col-sm-6 m-l-sm rightCheckBox">
<input type="checkbox" checked="checked" class="i-checks" name="PanelsContainer:tabsContentView:5:listTabs:rights-group-container:right-type-view:2:right-view:2:affected-right" disabled="disabled" id="id199"> <label>Delete group</label>
</div>
My problem is that the only unique identifier is:
<label>Delete group</label>
All other elements/id's/names are used by other checkboxes or changes from page to page.
I have tried the following code:
driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]").click()
But I only get error when using this.
Error: element not interactable
Anyone able to help with this?
Try the below xpath
//label[contains(text(), 'Delete group')]//ancestor::div//input
Try with Javascript.
checkBox = driver.find_element_by_xpath("//label[text()='Delete group']//ancestor::div//input")
# Scroll to checkbox if its not in screen
driver.execute_script("arguments[0].scrollIntoView();", checkBox)
driver.execute_script("arguments[0].click();", checkBox)
Note : As per HTML shared by you, checkbox is in Disabled state, so i am not sure click will trigger any action. However above code will click your checkbox.

Error Message: Unable to locate element. What may be the problem? Python 3.7, Selenium 3.141.0

When using selenium to find an element tag using Xpath, it works using the xpath copied from chrome for that element, this was to test if it was working. Since the id element changes every time I open the webpage I am aware that this will cause an error in my web automation program, now from that same tag I am using the class element as it does not change every time I open the webpage but Selenium fails to locate that element at all. When I change the element in xpath to class with its relative name from the xpath I initially copied and tested from the webbroswer. What may be the problem?
Here is a example of the html source code I am wanting to go to and the Xpath:
<div class="popover fade right in" role="tooltip" id="popover784483" style="top: -9px; left: 328.5px; display: block;">
<div class="arrow" style="top: 88.9423%;"></div>
<h3 class="popover-title">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Member details</font>
</font>
</h3>
<div class="popover-content">
<img class="center-block" src="http://api.vdarts.net:8080/picture/portrait/default.png" style="max-width:200px;height:auto;">
<hr>
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Account: CapitainJack </font>
Xpath intitially used but id changes:
//*[#id="popover784483"]/div[2]/font[1]/font
Xpath to be used:
//*[#class="popover fade right in"]/div[2]/font[1]/font
There are many ways to approach this issue.
You can match partial Text content Xpath like
//*[contains(text(), 'Account')]
You can use Img tag and following font like
(//img//following::font)[2]
I would recommend you use CSS selectors instead.
Compound class names are when an element has multiple classes separated by spaces. E.g. <div class="class1 class2 class3"></div>
if you want to select element with all 3 classes (in any order), you can use:
element = driver.find_element_by_css_selector(".class1.class2.class3")
If you want to use XPath, I have found that the following method works (using the same example as above):
browser.find_elements_by_xpath("//*[contains(concat(' ',#class,' '),' class1 ') and contains(concat(' ',#class,' '),' class2 ') and contains(concat(' ',#class,' '),' class3 ')]")
Note: you can use different boolean operators to filter out certain class names (e.g. 'and' 'and not' etc)

Python Selenium: Can't find an element on page

I'm pretty new to using python in selenium.
I have been trying to select a button on my web page. Here is the piece of HTML that appears after inspecting the element of the button:
<a class="btn col-xs-3 nav-btns" ui-sref="salt.dashboard.reports.minions" href="/dashboard/reports/minions/">
<span class="ssIcons-icon_reports salt-icon-3x ng-scope active" bs-tooltip="" data-title="Reports" container="body" placement="bottom" animation="none" data-trigger="hover" ng-class="{'active': state.current.name =='salt.dashboard.reports' … || state.current.name =='salt.dashboard.reports.minions'}">
::before
</span>
</a>
I have tried everything I can think of. Here are some of the things that I have tried:
element = driver.find_element_by_class_name("btncol-xs-3")
element = driver.find_element_by_name("Reports")
element = driver.find_element_by_id("Reports")
the error that I keep getting is:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: {"method":"class
name","selector":"salt.dashboard.reports"} Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/driver-component.js:10299)
at FirefoxDriver.prototype.findElement (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/driver-component.js:10308)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12282)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12287)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12229)
root#chris-salt:/home/chris/Documents/projects/python-selenium#
Find the element by data-title:
driver.find_element_by_css_selector("span[data-title=Reports]")
Or, if you need to get to the a tag:
driver.find_element_by_xpath("//a[span/#data-title = 'Reports']")
Chris,
The span that you pasted doesn't has an attribute named id.
Also, your class selector is too wide, i'd suggest using a more explicit path following the dom structure. Bare in mind that there may be multiple elements that have that class name.
Also, you are trying to find by the attribute name, which you don't have in that element.
Finally, it seems that you might be using angular. Does the input that you are looking for is created with javascript dinamically ?
And also, why are you using root to do this tests ?
Before doing the asserts, can you store the resulting html and manually checking that you indeed have that element?.

Categories