Selenium and HTML - python

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

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)

Finding/locating a clickable text by xpath in div/span format

I used the following line to click the Availability Grid button, but it failed to locate the element.
Class Sarsa-button-content is used everywhere so, I added text together to make it unique. However, it couldn't find it. What am I missing?
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='sarsa-button-content']/span[text()='Availability Grid']"))).click()
<div class="sticky-top-wrapper" style="top: 80px;">
<div class="site-filter-container" id="site-filter-container">
<a data-component="Button" class="sarsa-button view-by-availability-grid--button-tracker sarsa-button-primary sarsa-button-sm" href="/site/123456/availability">
<span class="sarsa-button-inner-wrapper">
<span class="sarsa-button-content">Availability Grid</span>
your xpath is wrong, try this:
//span[contains(#class, 'sarsa-button-content') and text() = 'Availability Grid']

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.

Python Selenium: clicking a "visible" element using Selenium gives me an "element not visible" error

I am trying to test against this website. (https://www.phptravels.net/), and I want to test its Login feature. There is a "My Account" link, which needs to be clicked first to show the drop down of the Login and Sign up button. The HTML code is like this:
<li id="li_myaccount" class="">
<span class="ink animate" style="height: 137px; width: 137px; top: -10.7969px; left: -28.7344px;"></span><i class="icon_set_1_icon-70 go-right"></i> My Account <b class="lightcaret mt-2 go-left"></b>
<ul class="dropdown-menu">
<li><a class="go-text-right" href="https://www.phptravels.net/login"> Login</a></li>
<li><a class="go-text-right" href="https://www.phptravels.net/register"> Sign Up</a></li>
</ul>
</li>
When I try to click the "My Account" button, it throws an error msg saying "element not visible". I am confused because apparently this button is visible all the time. Here is the code:
elem = driver.find_element_by_xpath("//*[#id='li_myaccount']/a")
elem.click()
What is wrong with my code? Thank you.
I tried to interact with the My Account Link, which is shown in the right part of the navigation bar on the page (https://www.phptravels.net/). Using the locator //*[#id='li_myaccount']/a when I try to click the Link, using the Webdriver, I get the below error:
ElementNotVisibleException: Message: element not interactable
When I explored the html using Chrome's console and searched the element using the locator //*[#id='li_myaccount']/a, the My Account Link, which you want to click doesn't get highlighted.
Therefore further exploration led me to choose the locator //*[contains(#class,'navbar-nav navbar-right')]//*[#id='li_myaccount']/a which highlights the My Account Link.
Then I used the new locator to click My Account link, using the Webdriver and it works !
If you notice carefully, I just added preceding path in the locator that you shared to uniquely identify the My Account Link.
Change the xpath to this one:
(//*[#id='li_myaccount']/a)[2]
If you look at the source, there are actually 2 elements matching this locator - an <a> tag in some modal that's currently hidden, and the one you are trying to address; thus your issue, the method returns the first one, which is not the desired.
This xpath will return the 2nd element ([2]) from the set of responses (the () surrounding the looked-for value).

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