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.
Related
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']
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).
I have used the below code to click the element.But it failed to locate the element and shows element not visible.
elem3=driver.find_element_by_xpath(".//*[#id='check-box']")
elem3.click()
The html code:
<span id="Some-span" class="urCWhl" title="Indicator">
<input id="check-box" class="urC" type="checkbox" hidefocus="hidefocus" ti="-1" tabindex="-1" ct="C"/>
<span id="label-lbl" class="name_class" style="width:100%;box-sizing:border-box;" unselectable="on" f="some-id" ti="0" tabindex="0" title="Indicator"></span>
You can try something like this:
element = driver.find_element_by_xpath(".//*[#id='check-box']")
driver.execute_script("arguments[0].click();", element)
The input might be inside a frame? If so switch to that frame by doing:
driver.switch_to_frame('framename')
or if not, try to find via id then click the element:
driver.find_element_by_id('check-box').click()
one thing to remember is that if the checkbox is already have a value, if you click the checkbox, the check will be removed. if you want to have the checkbox to have a true value always, you may do this:
driver.execute_script("document.getElementById('check-box').setAttribute('checked','');")
This will execute a javascript to always have a true value on the checkbox
I'm trying to click on a radio button on a web page which is dynamic. I tried classname, xpath and nothing works.
The code that contains the radio button is :
<input id="radiofield-1247-inputEl" class="x-form-field x-form-radio x-form-cb" type="button" hidefocus="true" autocomplete="off" role="radio">
There is a label which is adjacent to the button.
<label id="radiofield-1247-boxLabelEl" class="x-form-cb-label x-form-cb-label-after" for="radiofield-1247-inputEl">Yes</label>
I tried :
driver.find_element_by_xpath("//*[#type='radio'][#class='#class here'") #.click() and this doesn't work.
I tried passing the absolute xpath, which also fails. There is no name or id to do a search for.
You're trying type="radio" while on provided HTML it's type="button"...
Try following XPath:
driver.find_element_by_xpath('//input[contains(#id, "radiofield-")][#role="radio"]')
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