Parsing the HTML content using compound class using selenium python - 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")

Related

Selenium starts-with searchs entire page not in given Webelement

I want to search class name with starts-with in specific Webelement but it search in entire page. I do not know what is wrong.
This returns list
muidatagrid_rows = driver.find_elements(by=By.CLASS_NAME, value='MuiDataGrid-row')
one_row = muidatagrid_rows[0]
This HTML piece in WebElement (one_row)
<div class="market-watcher-title_os_button_container__4-yG+">
<div class="market-watcher-title_tags_container__F37og"></div>
<div>
<a href="#" target="blank" rel="noreferrer" data-testid="ios download button for 1628080370">
<img class="apple-badge-icon-image"></a>
</div>
<div></div>
</div>
If a search with full class name like this:
tags_and_marketplace_section = one_row.find_element(by=By.CLASS_NAME, value="market-watcher-title_os_button_container__4-yG+")
It gives error:
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression ".market-watcher-title_os_button_container__4-yG+" is invalid: InvalidSelectorError: Element.querySelector: '.market-watcher-title_os_button_container__4-yG+' is not a valid selector: ".market-watcher-title_os_button_container__4-yG+"
So i want to search with starts-with method but i can not get what i want.
This should returns only two Webelements but it returns 20
tags_and_marketplace_section = one_row.find_element(by=By.XPATH, value='//div[starts-with(#class, "market-watcher-")]')
print(len(tags_and_marketplace_section))
>>> 20
Without seeing the codebase you are scraping from it's difficult to help fully, however what I've found is that "Chaining" values can help to narrow down the returned results. Also, using the "By.CSS_SELECTOR" method works best for me.
For example, if what you want is inside a div and p, then you would do something like this;
driver.find_elements(by=By.CSS_SELECTOR, value="div #MuiDataGrid-row p")
Then you can work with the elements that are returned as you described. You maybe able to use other methods/selectors but this is my favourite route so far.

How to use selenium to click on this element?

How can I click on this element using selenium? I was tried using:(but was not successful)
divselected = driver.find_element_by_css_selector(div[aria-label="Add a public comment..."])
divselected.click()
I also tried
divselected = driver.find_element_by_id("contenteditable-root")
divselected.click()
The div I am trying to click:
<div id="contenteditable-root" contenteditable="true" dir="auto" class="style-scope yt-formatted-string" aria-label="Add a public comment..."></div>'
The following code works:
divselected = driver.find_elements_by_xpath("//*[contains(text(), 'Add a public comment...')]")
divselected[0].click()
Hope this help:
divselected=driver.find_element_by_xpath("//*[contains(text(),'Add a public comment')]")
incorrect value for the id. The corrected code uses the correct id value, contenteditable-root to #contenteditable-root
try this code :
divselected = driver.find_element_by_css_selector("#contenteditable-root")
divselected.click()
This code finds the element with the id attribute contenteditable-root using a CSS selector and then performs a click action on it using the click() method.
try using
driver.find_element(By.ID, 'contenteditable-root').click()
or instead of ID use Class Name.

How to click on a button with Selenium?

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

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?.

Using SST framework, how do you click a link by css class or xpath?

I am testing a website that has buttons that are not discernible by element or id. However, I am able to identify it by xpath or css class. How do you click on a button using one of those two attributes. I'm able to assert the button is there using get_element_by_xpath; however, I seem unable to use the click_element, click_link, or click_button using id_or_elem. What statement would you use to click on a button like this?
Edit:
The test code looks like this: (webpage name removed on purpose)
class TestMyTest(cases.SSTTestCase):
def test_mytestcase_home_page(self):
go_to('http://www.mywebpage.com')
assert_title_contains('Page Name')
assert_element(tag='a', text='Log in')
click_element(id_or_elem='page_nav')
assert_title_contains('Login')
assert_element(id='tbLUser')
write_textfield('tbLUser','username')
assert_element(id='tbLPass')
write_textfield('tbLPass','badpassword')
assert_element(css_class='btn-login1')
get_element_by_xpath('//*[#id="login_box"]/div/a/img')
The code I'm trying to test from:
<div class="btn-login1">
<a href="javascript:void(0)" onclick=javascript:return Validate(document.theLoginForm,'/mypage/mywebpage.asp');" onmouseout="MM_swapImgRestore();" onmouseover="MM_swapImage('lg5_login','','/assets/page4/lg5_login_f2.png',1);">
<img name="lg5_login" src="http://www.mywebpage.com/login/lg5_login.png" border="0" alt="Log in Now">
</a>
</div>
If this answer is helpful to someone else. We we able to solve the issue with the following solutions.
click_element(get_element_by_xpath('your xpath'), wait=True)
click_link(get_element_by_xpath('your xpath'), wait=True)
click_button(get_element_by_xpath('your xpath'), wait=True)
Parameters: id_or_elem The identifier of the element, or its element object.
All of the click actions should support id or element

Categories