This is my code:
I have used the find element by id RESULT_RadioButton-7_0, but I am getting the following error:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="/home/real/Desktop/Selenium_with_python/SeleniumProjects/chromedriver_linux64/chromedriver")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")
radiostatus = driver.find_element(By.ID, "RESULT_RadioButton-7_0").click()
My error is this:
elementClickInterceptedException: element click intercepted: Element is not clickable at point (40, 567). Other element would receive the click: <label for="RESULT_RadioButton-7_0">...</label> (Session info: chrome=78.0.3904.70)
Based on the page link you provided, it looks like your locator strategy is correct here. If you are getting an error—most likely NoSuchElementException, I am assuming it might have something to do with waiting for the page to load before attempting to find the element. Let's use the ExpectedConditions class to wait on the element to exist before locating it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Add the above references to your .py file
# Wait on the element to exist, and store its reference in radiostatus
radiostatus = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))
# Click the element
#radiostatus.click()
# Click intercepted workaround: JavaScript click
driver.execute_script("arguments[0].click();", radiostatus)
This will tick the radio button next to "Male" on the form.
Please find the below answer which will help you to click on the "Male" radio button from your link.
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
driver.get('https://fs2.formsite.com/meherpavan/form2/index.html?153770259640')
# Clicking on the "Male" checkbox button
maleRadioButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))
ActionChains(driver).move_to_element(maleRadioButton).click().perform()
Unless you need to wait on the element (which doesn't seem necessary), you should be able to do the following:
element_to_click_or_whatever = driver.find_element_by_id('RESULT_RadioButton-7_0')
If you look at the source for find_element_by_id, it calls find_element with By.ID as an argument:
def find_element_by_id(self, id_):
return self.find_element(by=By.ID, value=id_)
IMO: find_element_by_id reads better, and it's one less package to import.
I don't think your issue is finding the element; there's an ElementClickInterceptedException when trying to click on the element. For example, the radio button is located, but (strangely) Selenium doesn't think it's displayed.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")
radiostatus = driver.find_element_by_id('RESULT_RadioButton-7_0')
if radiostatus:
print('found')
# Found
print(radiostatus.is_displayed())
# False
Related
I'm trying to click this button shown after choosing an option from a drop-down menu.
This is the button I'm trying to click.
The link to the website: https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx
The html:
I've tried using XPATH, and through visible text; nothing seems to work.
My code as of now:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
drop_dist = \
driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[1]/div[2]/div/select")
select_dist = Select(drop_dist)
select_dist.select_by_value("005")
l = driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[4]/div/div/table/tbody/tr[1]/td/div/div[2]/div[2]/div[1]/a").click()
time.sleep(30)
Any help is much appreciated!
The locator seems fragile, use following locator and webdriverwait() to handle sync issue.
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
wait=WebDriverWait(driver, 20)
select_dist =Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_ddl_District"))))
select_dist.select_by_value("005")
wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[#class='btn btn-link' and contains(., 'View Detail Break up')])[1]"))).click() //this will click the first one only.
You have to add 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
Try this:
button = driver.find_elements(By.CSS, 'a[role="button"][aria-expanded="false"]')[0]
button.click()
You can change the index in order to click on other elements
Code trials:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from webdriver_manager.chrome import ChromeDriverManager
import time
# Find Search Element end Type Automation
search_bar = driver.find_element(By.XPATH, "//input[#id='sb_form_q']")
search_bar.send_keys("Automation")
time.sleep(3)
# Click Search Button
search_button = driver.find_element(By.XPATH, "//label[#id='search_icon']//*[name()='svg']")
search_button.click()
time.sleep(3)
# Clear Search Bar
search_bar.clear()
Why when I try to reuse search_bar( e.g:search_bar.clear() ) I always encouter problem:
StaleElementReferenceException Message: stale element reference: element is not attached to the page document
(Session info: chrome=104.0.5112.81)
Help please.
Error snapshot:
Error snapshot:
After you do search_button.click() DOM seem to be updated and so you need to re-define your search_bar:
# Click Search Button
search_button = driver.find_element(By.XPATH, "//label[#id='search_icon']//*[name()='svg']")
search_button.click()
time.sleep(3)
# Clear Search Bar
search_bar = driver.find_element(By.XPATH, "//input[#id='sb_form_q']")
search_bar.clear()
Once you identify the search_button and invoke click on it, the search results appear on the page. This change within the DOM Tree also effects the previously located(found) elements, e.g. search_bar.
Hence, when you try to refer the previously located search_bar element, you see the error:
StaleElementReferenceException Message: stale element reference: element is not attached to the page document
Solution
To click, clear or send_keys you have to locate the desired element afresh as follows:
search_bar = driver.find_element(By.XPATH, "//input[#id='sb_form_q']")
Ideally inducing WebDriverWait for the element_to_be_clickable() as follows:
search_bar = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='sb_form_q']")))
I am trying to get selenium to scroll down to a button in a website that has the text 'Try it out!' inside the button.
My problem is that there are no uniquely ID'd elements around the button to which I could scroll the view to. In addition, when I inspect the website with dev tools and search from the text 'Try it out!' in the HTML I get 72 results. I figured out that I need the 18th button but I am unable to get the browser to scroll to the button. Instead I get an error saying "The provided double value is non-finite".
Could you please look at the code below and give me an explanation to why I the browser is not scrolling down to the button?
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
import pathlib
# Get path to chromedriver
file_path = pathlib.Path(__file__).parent.absolute()
chromedriver_path = str(file_path)+"\\chromedriver.exe"
class Scraper:
def __init__(self):
# Open website
self.driver = webdriver.Chrome(chromedriver_path)
print(self.driver)
self.driver.get(
"https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages")
sleep(5)
# Get the 18th button that says 'Try it out!'. Position()=17 because starts with 0.
element = self.driver.find_element_by_xpath(
'(//input[#value="Try it out!"])[position()=17]')
# Scroll to the button and click it
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element.click()
sleep(5)
Scraper()
To grab the Try it out button and click on it first create a webdriver wait to wait for the element to be clickable.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='devices_get_devices_dev_selector_messages_content']/form/div[3]/input")))
element.click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Please try the below code.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get('https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages')
Try_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#devices_get_devices_dev_selector_messages_content > form > div.sandbox_header > input')))
action.move_to_element(Try_btn).click().perform()
This code works for me.
This error message...
Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
...implies that the WebDriver instance was unable to find the element for one or the other reasons:
The element haven't loaded properly when you tried to interact with it.
Element is within an <iframe> / <frame>
The style attribute of the element contains display: none;
Element is within an shadow DOM
You can find a relevant detailed discussion in javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
This use-case
Among the 72 elements with text as Try it out! i.e.
<input class="submit" type="submit" value="Try it out!" data-sw-translate="">
barring the desired element all the other 71 elements have an ancestor with style attribute set as display: none; overflow: hidden;. Where as only the desired element is having style attribute set as overflow: hidden;.
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:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#devices_get_devices_dev_selector_messages_content input[value='Try it out!']"))).click()
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='devices_get_devices_dev_selector_messages_content']//input[#value='Try it out!']"))).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
There are HTML elements on the website (https://www.immowelt.de/expose/2ult44w) that Selenium does not recognize at all. But I would like to be able to address them. I still recognize the element "body" without any problems, however "div [# class = 'cdk-overlay-container']" is not. Errors are not thrown.
from selenium import webdriver
import time
driver = webdriver.Chrome('C:\\go2\\installation\\chromedriver.exe')
driver.get("https://www.immowelt.de/expose/2ult44w");
driver.execute_script("return document.readyState") == "complete"
time.sleep(10)
#just so that a message is clicked away:
datenschutz = driver.find_elements_by_xpath("//button[#id='uc-btn-accept-banner']")
if len(datenschutz) > 0: datenschutz[0].click()
#that is not recognized:
example = driver.find_elements_by_xpath("//div[#class='cdk-overlay-container']")
print("Counts:"+str(len(example))) #result: Counts:0
The reason you are unable to found it because the element is present inside an iframe.
You need to switch to iframe first.
To Switch to iframe
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
And
Induce WebDriverWait() and wait for presence_of_all_elements_located()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('C:\\go2\\installation\\chromedriver.exe')
driver.get("https://www.immowelt.de/expose/2ult44w")
btn=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='uc-btn-accept-banner']")))
driver.execute_script("arguments[0].click();", btn)
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"externalViewerStage")))
example=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//div[#class='cdk-overlay-container']")))
print("Counts:"+str(len(example)))
driver.switch_to.default_content()
https://www.sevenonemedia.de/tv/programm/programmwochen
Here I want to login:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = Options()
chrome_path = "T:/Markus/WebScrapingExample/Chromedriver/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path,chrome_options=options)
driver.set_window_size(1280, 720)
time.sleep(5)
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_xpath('//*[#id="sign-in-button"]').click()
ElementNotInteractableException: element not interactable
(Session info: chrome=78.0.3904.97)
This is my error
Id is there. Why does this happen?
This page contains duplicate of element with ID sign-in-button. If your selector points to more than one element, driver always takes the first from the top of the DOM one which is not interactable in this case. You must refer to the second element with this id. Try this selector for "Sign in" button:
//*[#id="aheadcustom_p_p_id_58"]//button
hi first things you should not give your password to all of the stackoverflow community :)
you can't click on the button because there is a popup at the bottom of the page and you have to click on it first for selenium it's hidding your button
last is that the totality of your code ?
if yes you forgot
driver = webdriver.Firefox() #or any other webdriver
you have not create driver without this line
EDIT !!
it wasn't working with only the modification above but with this one its good
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
time.sleep(5)
driver = webdriver.Firefox()
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("login")
driver.find_element_by_id("_58_password").send_keys("pssd")
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/a").click()
driver.find_element_by_css_selector("#_58_fm > fieldset:nth-child(1) > div:nth-child(6) > button:nth-child(1)").click()
this work :)
sometime css selector a safer and work better
To click on the Login button you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using XPATH:
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Anmelden' and not(contains(#name,'INSTANCE'))]"))).send_keys("a.mai#mediaplus.com")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]").send_keys("Edidaten17")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]//following::button[1]").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:
The problem is that your button locator isn't unique on the page. It finds two buttons, the first of which is not visible which causes the ElementNotInteractableException.
The simple fix is to use the CSS selector below
#main-content #sign-in-button
That will find only the button you want. So your last few lines of code would be
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_css_selector('#main-content #sign-in-button').click()
sleep(1)
login_box = driver.find_element_by_name('login')
login_box.click()
this is for facebook login button, just inspect the website and see the id/name/type to make your code automate to work properly