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()
Related
I am trying to access website with python selenium. I am going to place that I cant click on the button. any help please?
please find my code on the below:
from selenium import webdriver
from time import sleep
path_to_chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(path_to_chromedriver)
url = 'https://www.mail.com/'
driver.get(url)
how to click on button "Agree and Continue" using css selector ?
The element Agree and Continue is present inside nested iframe you need to switch both the iframe first and then click on the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Induce WebDriverWait() and wait for element_to_be_clickable()
driver.get("https://www.mail.com/")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Jump out from iframe you need to use.
driver.switch_to.default_content()
I am facing inconsistencies in Selenium execution.
Last line in the code snippet I pasted below doesn't execute consistently. Sometimes it works, sometimes it throws an error saying that element is not found. Doesn't Selenium "block" for the element to appear before attempting to execute the click? I generated it using Selenium IDE. What I am missing here?
self.driver.find_element(By.CSS_SELECTOR, ".dx-ellipsis:nth-child(2)").click()
self.driver.switch_to.default_content()
self.driver.find_element(By.CSS_SELECTOR, "#PageContentPlaceHolder_TimeControlSplitter_TimeControlContent_TimesheetEntrySplitter_TimesheetDetailsMenu_DXI0_T > .dxm-contentText").click()
Selenium may not find elements if they happen to be loaded dynamically by JS and if you search for them before they are loaded.
You can try either an implicit wait or an explicit wait.
In case of implicit waiting, the docs say:
An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available.
You could do with something like:
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10) #wait and poll for 10 seconds
Whereas the explicit waiting means to explicitly specify the element which is to be waited for it to be available. As per the docs:
An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.
You can do this with something like:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
element1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".dx-ellipsis:nth-child(2)")))
element1.click()
self.driver.switch_to.default_content()
element2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#PageContentPlaceHolder_TimeControlSplitter_TimeControlContent_TimesheetEntrySplitter_TimesheetDetailsMenu_DXI0_T > .dxm-contentText")))
element2.click()
As you are using the line of code:
self.driver.switch_to.default_content()
Presumably you are switching Selenium's focus from a frame or iframe to the Top Level Content. Hence you need to induce WebDriverWait for the desired element to be clickable and you can use the following Locator Strategy:
self.driver.switch_to.default_content()
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#PageContentPlaceHolder_TimeControlSplitter_TimeControlContent_TimesheetEntrySplitter_TimesheetDetailsMenu_DXI0_T > .dxm-contentText"))).click()
Note:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant detailed discussions in:
How to send text to the Password field within https://mail.protonmail.com registration page?
How to switch between iframes using Selenium and Python?
Wait for the element to be loaded
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dx-ellipsis:nth-child(2)"))).click()
self.driver.switch_to.default_content()
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#PageContentPlaceHolder_TimeControlSplitter_TimeControlContent_TimesheetEntrySplitter_TimesheetDetailsMenu_DXI0_T > .dxm-contentText"))).click()
The number is how long the driver should spend looking for the element before moving on.
i'm having a problem with selenium. I load this page: Investing 3M, and i want to click got it in a pop-out window.
I already tryed with this code bellow, but it doesn't work:
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
driver.implicitly_wait(10)
x =driver.find_elements_by_xpath("/html/body/div[8]/div[1]/div/div[2]/div[2]/a[1]")
print(len(x))
print(x)
Can anyone provide any solution to this?
The element is present inside an iframe.You need to switch to frame first.
To handle dynamic element you need to
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following xpath
Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath
code:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='TrustArc Cookie Consent Manager']")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Got it']"))).click()
Browser snapshot after clicking
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
I am trying to switch into a new frame, which opens after clicking a button. Unfortunately, I receive a None value.
driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
time.sleep(5)
driver.find_element_by_xpath('//button[#id="pagAnuShowContactForm"]').click()
time.sleep(5)
Till here, it works to open the contact information. Here, I would like to perform actions in the new, opened window.
I tried the following options:
1.
contact = driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
2.
contact = driver.find_element_by_xpath('//div[#class="telefonos"]')
To get the telephone number.The element is present inside iframe ID ifrw you need to switch to iframe first.
Induce WebDriverWait And frame_to_be_available_and_switch_to_it()
Induce WebDriverWait And visibility_of_element_located()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='pagAnuShowContactForm']"))).click()
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrw")))
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".telefonos"))).text.strip())
driver.close()
Output:
663473583