I'm trying to tick all checkboxes on a page. There are about 50 of them and this code seems to work but only ticks what is visible on the screen. It doesn't scroll down the table and tick the remaining boxes.
def select_checkbox():
checkboxes = driver.find_elements_by_css_selector("//input[#type='checkbox']")
for checkbox in checkboxes:
if not checkbox.is_selected():
checkbox.click() # to tick it
I'm thinking It's only finding those on the screen as the rest are still loading. I need to wait until all checkboxes have loaded but I'm not sure about how to go about this. Any help is appreciated.
Below is the table row
<input type="checkbox" value="1" name="admin_contract_form[price_forms_attributes][51][offered]" id="admin_contract_form_price_forms_attributes_51_offered">
You can use explicit waits presence_of_all_elements_located for all the checkboxes.
Also, to scroll driver.execute_script("arguments[0].scrollIntoView(true);", checkbox) this will likely to get the job done.
Code:
wait = WebDriverWait(driver, 30)
def select_checkbox():
checkboxes = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//input[#type='checkbox']")))
for checkbox in checkboxes:
driver.execute_script("arguments[0].scrollIntoView(true);", checkbox)
if not checkbox.is_selected():
checkbox.click() # to tick it
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Select all the desired checkbox inducing WebDriverWait for the visibility_of_all_elements_located() and click() the individual checkboxes inducing WebDriverWait for the element_to_be_clickable() as follows:
def select_checkbox():
checkboxes = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[starts-with(#name, 'admin_contract_form') and starts-with(#id, 'admin_contract_form_price_forms_attributes') and not(#disabled)]")))
for checkbox in checkboxes:
if not checkbox.is_selected():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(checkbox)).click()
PS: click() inducing WebDriverWait will scroll the element into the view by default.
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
I am trying to use selenium to click certain buttons within the bank of america simulator, but the buttons don't seem to ever click. No new link is reached, which is something I haven't encountered before.
https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/
I want to click "Sign in options" and then click "Sign in: Recognized device"
I tried using selenium to click the button and I get no error. Nothing happens at all and the program continues, so I know it's not an issue with not finding the button. My current code is as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
sleep(3)
login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);
This code worked fine for me.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()
NOTE: Using sleep is a bad practice, use WebDriverWait and wait for the specific state you need instead.
To click on the clickable elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Code block:
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
I'm trying automating a website and I'm trying to access a menu > submenu and perform a click on the item in submenu. After a lot of search I learned that mouseover is a better option for me as the submenu only appear if I hover the mouse on the menu. I have tried many instances but the mouse just won't hover on the main menu and to submenu. The code is:
policymngmt = driver.find_element_by_xpath('/html/body/app-root/div/div/main/site-header/div[1]/div[1]/div[2]/div/div/div/div/div[2]/ul/li[2]/a')
issuance = driver.find_element_by_xpath('/html/body/app-root/div/div/main/site-header/div[3]/div/div[1]/nx-link/a')
actions = ActionChains(driver)
actions.move_to_element(policymngmt).move_to_element(issuance).click().perform()
Try the below
actions = ActionChains(driver)
policymngmt = driver.find_element_by_xpath("xpath")
hover= ActionChains(driver).move_to_element(policymngmt)
hover.perform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/app-root/div/div/main/site-header/div[3]/div/div[1]/nx-link/a"))).click()
Imports
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
I want to close the popup window that appears when I hit a particular url. Here is the "inspect element" window of that page:
Here is what I have tried:
driver.find_element_by_css_selector("i[#class='popupCloseIcon']").click()
But it gives following error:
InvalidSelectorException: Message: Given css selector expression
"i[#class='popupCloseIcon']" is invalid: InvalidSelectorError:
Document.querySelector: 'i[#class='popupCloseIcon']' is not a valid
selector: "i[#class='popupCloseIcon']"
Here is the url where popup appears: https://www.investing.com/equities/oil---gas-dev-historical-data
Once the url is opened through selenium, the pop up appears after a few seconds.
How can I click that close button?
The popup appears after some time, so you need wait to solve this. And you have invalid selector : i[#class='popupCloseIcon'], please use i[class*='largeBannerCloser']
Try the below:
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
try:
popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
popup.click()
except TimeoutException as to:
print(to)
This is wait until 60 seconds maximum.
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
As the pop up appears after a few seconds accessing the url https://www.investing.com/equities/oil---gas-dev-historical-data to to close the popup window you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.popupCloseIcon.largeBannerCloser"))).click()
Using XPATH:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//i[#class='popupCloseIcon largeBannerCloser']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
First, "i[#class='popupCloseIcon']" is a invalid css selector locator, it should be "i[class='popupCloseIcon']". Second, there are four elements mapped to the "i[class='popupCloseIcon']", the css selector "div.right>i.popupCloseIcon" will help you to locate the target element
I tried this code to select a radio button:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.asiamiles.com/en/enrolment.html')
gender = driver.find_element_by_id("gender_Female")
gender.click()
I received this error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Where could the problem be, and how can I solve it?
To click() on the radio button for female you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.asiamiles.com/en/enrolment.html")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"label[for='gender_Female']"))).click()
Using XPATH:
driver.get("https://www.asiamiles.com/en/enrolment.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='gender_Female']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reason for getting ElementNotInteractableException is that the radio button coordinates are x=0 and y=0, so selenium is not able to click.
Please use this below approach.
imports needed:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Script:
url = 'https://www.asiamiles.com/en/enrolment.html'
driver.get(url)
female = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.CSS_SELECTOR,'.radio-label.bodytext4.F')))
female.location_once_scrolled_into_view
female.click()
only using id it wont work when you hover on label tag it shows shaded region of the radio button that's where you need to click. for addition to that it takes a while to load a page so for smooth action use action class and it will click on element.
actions = ActionChains(driver)
gender = driver.find_element_by_xpath(".//input[#name='gender']/following-sibling::label[#for='gender_Female']")
actions.move_to_element(gender).perform()
I am trying to click a button on webpage using selenium script but it is giving me following error using this line:
driver.find_element_by_class_name('btn-primary').click()
Error is as follows:
ElementNotInteractableException: Message: Element <button class="btn-primary btn-text sort-filter-clear-button" type="button"> could not be scrolled into view
HTML of the button element:
<button type="submit" class="btn-primary btn-action bookButton" id="bookButton" data-track="FLT.RD.Book.Bottom"><span class="btn-label">Continue Booking</span></button>
Try to wait for element:
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
It will wait at least 10 seconds, until element will be clickable.
Note: you have to add some exports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
EDIT: you can try also js executor like this:
button = driver.find_element_by_id("bookButton")
driver.execute_script("arguments[0].click();", button)
in case your button is inside an iframe/frame, firstly you have to switch to this frame and only then you can interact with this element:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME("frame_name"))))
# do your stuff
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
driver.switch_to.default_content() # switch back to default content
As per the HTML you have shared and as you mentioned of bootstrap button, moving forward as you are trying to invoke click() on the desired element you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-primary.btn-action.bookButton#bookButton>span.btn-label"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn-primary btn-action bookButton' and #id='bookButton']/span[#class='btn-label'][contains(.,'Continue Booking')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC