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
Related
I want to click this button in a website using selenium but I am not sure how to select it.
<a data-gfm-analytics-element="btn_showmore_browse" data-area="norma_category" class="button hollow expanded-mobile js-load-more-results" href="#" style="display: inline-block;">Show More</a>
I used this code to do it but it returns NoSuchElementException: Message: no such element
btn = driver.find_element(By.CLASS_NAME,'button hollow expanded-mobile js-load-more-results')
button hollow expanded-mobile js-load-more-results are actually 4 class names while By.CLASS_NAME receives single class name value. You can use CSS Selector or Xpath with multiple class names
btn = driver.find_element(By.CSS_SELECTOR,'a.button.hollow.expanded-mobile.js-load-more-results')
Also btn_showmore_browse seems to be unique value, so possibly this will work too:
btn = driver.find_element(By.CSS_SELECTOR,'a[data-gfm-analytics-element="btn_showmore_browse"]')
There are more possible options
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.
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
I am using Selenium in python to click a button in a dialog. I try to click"OK", but it keeps getting errors
the buttons show "display:block" in CSS, cause "element is not visible" error
find_element_by_xpath, but the xpath of the element keeps changing
the class names are the same, how to choose the "OK" button?
Here are the code
<div<class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<div<class="ui-dialog-buttonset">
<button type="button" class="large ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
<span class="ui-button-text">Cancel</span>
</button>
<button type="button" class="orange large ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
<span class="ui-button-text">OK</span>
</button>
</div>
</div>
Thank you:)
If the element is invisible in the DOM like css { display: None}, {opacity: 0}... etc, , selenium will not be able to SEE it even given that you try to wait or time.sleep, instead you should use execute_script to run a JavaScript to trigger the desired event, something like this:
driver.execute_script('document.querySelector("span.ui-button-text").click();')
You should wait for the element to be ready before trying to click on it. Use waitForVisible or similar to achieve that.
For example, something like this:
element = WebDriverWait(driver, 10).until(
lambda driver : driver.find_element_by_xpath(element_xpath)
)
If the class remains the same, then you should select that class with element_xpath. The last thing you need to determine is what other attribute designates the button as ready. Then you can wait for the specific argument like:
def find(driver):
e = driver.find_element_by_xpath(element_xpath)
if (e.get_attribute(some_attribute)==some_value):
return False
return e
element = WebDriverWait(driver, 10).until(find)
Two different things can happen here.
• The selector is not explicitly returning the intended element
• Element load issue.
If both cases are true use explicit wait with a correct selector. In terms of selector I like using text bases search in such scenario. Notice I am using xpatth contains to make sure it eliminates any leading or tailing white spaces.
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH , "//span[contains(text(),'OK')]")))
API doc here
The webpage i'm starting from is https://classschedule.tulane.edu/Search.aspx . The page source information for the button I need clicked is:
<input type="submit" name="ctl00$MainContent$btnSearchAll" value="All Courses" id="btnSearchAll" class="JQButton ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false" autocomplete="off" style="height: 22px;">
I have tried different methods to find this button and click on it such as;
element = browser.find_element_by_id("btnSearchAll")
element = browser.find_element_by_xpath("//input[#id ='btnSearchAll']")
element = browser.find_element_by_name("ctl00$MainContent$btnSearchAll")
I think it is finding the button because when I do...
print element
...this is returned:
<selenium.webdriver.remote.webelement.WebElement object at 0x2b49690>
I have no other ideas on how to make my program click the button.
I think you didn't add the code for clicking on the element. Please do that and check:
element = browser.find_element_by_id("btnSearchAll")
element.click()
I did try the same code as above with JAVA, and it is working fine.
This works well for me
for i in range(1, 10):
try:
driver.find_element_by_xpath(
f'/html/body/div[{i}]/div/div/div/div/div[2]/span[1]/span/span/input').click()
except:
pass