How to click on a button with Selenium? - python

I am trying to use the Python Selenium API in order to click on a button. The HTML code is as follows:
<button class="btn wizard-next btn-primary" type="button">Weiter</button>
How to best identify this element? I was trying the following code
driver.find_element_by_class_name("btn wizard-next btn-primary").click()
but got an error
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
What else can I do to select this element?

You cannot use find_element_by_class_name() if class name value contains spaces.
Try:
driver.find_element_by_xpath("//button[#class='btn wizard-next btn-primary']").click()

You can use css selector also
driver.find_element_by_css_selector("btn").click()

Related

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.

Parsing the HTML content using compound class using selenium python

I am having a display button in my GUI that shows the connection status (Button with Green check means connection is established and with Red cross means no connection)
I have to check the status using my code.
I am parsing the content of that particular title-bar class name (container-fluid).
And from this, I am parsing the explicit content of that display button.
elem = driver.find_element_by_class_name("container-fluid")
a= elem.get_attribute("outerHTML")
b= a.split("powerOn icon-ok-sign")
After this, I parse some explicit content of that button and decide that connection is there or not.
But If I use class="powerOn icon-ok-sign", I get error :
Compound class names not permitted
<div class="powerOn icon-ok-sign" data-original-title="Connection" style=" font-size: 2em;" data-toggle="tooltip" title="" data-placement="bottom" ng-class="{"powerOn icon-ok-sign": titleArea.systemStatus.connection.value, "powerOff icon-remove-sign" : !titleArea.systemStatus.connection.value}"></div>
But If I use class="powerOn icon-ok-sign", I get error Compound class names not permitted
Actually selenium doesn't support to locate an element using Compound class name.
You should try using on of then instead as :-
driver.find_element_by_class_name("powerOn")
Or
driver.find_element_by_class_name("icon-ok-sign")
Or best way to use css_selector to locate same element using multiple class name as :-
driver.find_element_by_css_selector(".powerOn.icon-ok-sign")
Reference link :-
Compound class names are not supported error in WebDriver
How to avoid Compound Class name error in Page Object?
You can use search by CSS Selector instead:
driver.find_element_by_css_selector(".powerOn.icon-ok-sign")
or use one of class names to select your element:
driver.find_element_by_class_name("powerOn")

How to retrieve text from a button type webelement

I am trying to retrieve text from a button type webelement using Python scripting in Selenium
The HTML code of the button looks like:
<button class="list-item ng-binding" ng-click="selectLineFilter(line)" type="button">
<i class="mdi mdi-domain blue" aria-hidden="true"></i>
12063497545
</button>
How can I retrieve the text. I used .text, it returns an error as " 'list' object has no attribute 'text' ". Please Help.
I think you are using find_elements which returns list of WebElement while .text works on single WebElement, So you should try using find_element as below :-
driver.find_element_by_css_selector("button.list-item.ng-binding[type='button']").text
Or if you want to find all elements text with the same locator, you should try using find_elements and iterate through in loop as below :-
buttons= driver.find_elements_by_css_selector("button.list-item.ng-binding[type='button']")
for button in buttons:
button.text
use .GetAttribute("innerHTML")
you can use the concept of this Java code :
WebDriver driver;
String a = driver.findelement(by.xpath("")).getText(); //use css or id attribute
System.out.println(a); //this will print the text present on the button.

Selenium find element and click on it

I'm trying to get Selenium to click on View All Companies button, but i'm not sure what am I doing wrong. It returns no element found
html code
<div class="screener-toggles">
<div class="buttons">
<span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span>
<span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span>
<span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span>
</div>
</div>
python code I wrote
element1 = driver.find_elements_by_class_name('View All Companies')
element1.click()
# I have tried all-companies instead of View All Companies as well. But still doesn't work
Should I not be using find_elements_by_class_name?
Any advice on what I am doing wrong is greatly appreciated!
try xpath: "//span[contains(text(),'View All Companies')]"
View All Companies is text, not the class. Try looking by text with css_selector or xpath
element1 = find_element_by_css_selector('span:contains("View All Companies")')
element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]')
Or by the data-name attribute which contains all-companies
element1 = find_element_by_css_selector('span[data-name*="all-companies"]')
Yes, you should not use the find_elements_by_class_name instead of use find_element_by_class_name.
find_elements_by_class_name is used when your expecting your locator to return more than 1 element. for a specific element use only find_element_by_class_name.
Another thing is I am not able to see any class name as View All Companies in your HTML code. Please look into your HTML and select classname or other locator carefully
Hope it will help you

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