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()
Related
I am trying to press a button with selenium. The page comes up, but it does not press the button. I am new to this, and also the page as a GEO block for any one in the UK. I am using a windows 10 laptop.
This is the code I have so far:
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome(executable_path = r'G:/scraping_practice/chromedriver_win32/chromedriver.exe')
driver.get('https://www.maxpreps.com/tx/basketball/21-22/stat-leaders/scoring/ppg/')
search_button = driver.find_element(By.xpath('/html/body/div[1]/div[4]/div[1]/div/div[2]/div[3]/div/div/ul/li[2]/button'))
search_button.click()
To click on the element with text 2 you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='controls']//ul/li/button[text()='2']"))).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
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 want to ask aobut driver.find_element(). I want to make the automatic site login with a chrome driver and python. I want to click the login button, but it doesn't work.
Here is code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://www.naver.com')
time.sleep(2)
search = driver.find_element(By.CLASS_NAME,'link_login')
search.click()
I also used
search = driver.find_element(By.CLASS_NAME,'link_login')
but it didn't work too. How can I make it to work?
You were close. To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CLASS_NAME:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "link_login"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.link_login"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='link_login']"))).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
I'm trying to write a script to fill out a form but I'm struggling to click the free sim link, I have tried using multiple different identifiers but can't seem to get any to work. Greatly appreciate any help! Thank you
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.webdriver.common.action_chains import ActionChains
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://mobile.lebara.com/gb/en/free-sim")
driver.implicitly_wait(5)
cookie = driver.find_element_by_id("onetrust-accept-btn-handler")
freesim = driver.find_element_by_class_name("product-item payAsYouGoProductListerItem clickable")
actions = ActionChains(driver)
actions.click(cookie).perform()
driver.implicitly_wait(5)
actions.click(freesim).perform()
You have to deal with "accept cookies" banner (click on it, you don't need actions, only EC then click)
replace all implicit waits in your code with expected_conditions
(In case all these recommendations don't work for you have to update your question with code and selenium exceptions)
You need to click "Accept cookies" or execute JS code to hide "Your cookies" overlay and then search for link.
driver.implicitly_wait(5)
cookie = driver.find_element_by_id("onetrust-accept-btn-handler")
cookie.click()
freesim = driver.find_element_by_link_text("Free Sim")
freesim.click()
Note that you're trying to pass multiple class names to find_element_by_class_name (you need to pass one only) and call driver.implicitly_wait(5) several times (you might do this only once)
Your class name contains spaces and thus are actually multiple class names. You can make a CSS selector by prefixing each class name with a dot.
CSS Selector ->> ".product-item.payAsYouGoProductListerItem.clickable"
You also have to wait until the element is clickable before you click. Add this to/change this in your code. The rest of your code works fine.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
....
....
....
freesim = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".product-item.payAsYouGoProductListerItem.clickable")))
freesim.click()
To click on the element Free Sim you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button#onetrust-accept-btn-handler").click()
Using xpath:
driver.find_element_by_xpath("//a[#href='free-sim-direct']").click()
Ideally, to click on the element you need 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://mobile.lebara.com/gb/en/free-sim")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='free-sim-direct']"))).click()
Using XPATH:
driver.get("https://mobile.lebara.com/gb/en/free-sim")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='free-sim-direct']"))).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
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