I m unable to locate the frame - tabFrame from the below screenshot - python

I'm unable to locate the frame - tabFrame from the attached screenshot, whereas I was able to locate outlookFrame. I used:
driver.switch_to.frame('outlookFrame')
But when I use:
driver.switch_to.frame('tabFrame')
It doesn't work.

As the desired elements is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("http://web_page")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"outlookFrame_cssSelector")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='tabFrame']")))
Using XPATH:
driver.get("http://web_page")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"outlookFrame_xpath")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='tabFrame']")))
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
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
How to write appropriate Xpath to locate text value
How To sign in to Applemusic With Python Using Chrome Driver With Selenium

Related

Selenium Python: Not finding element which clearly exists

I am trying to click through the levels of a site's navigation using python and selenium. The navbar contains list items that have subelements within them.
Here is the html of the navbar:
The objective here is to find the element with id="ts_time", to hover over it and to click on the element within it.
So far I have tried the following selection types:
ID
XPath
Class_Name
Here is the ID.
time_menu_button = driver.find_element(By.ID, "ts_time")
ActionChains(driver).move_to_element(time_menu_button)
time.sleep(2.5)
This results in a NoSuchElementException
To hover over the element with id="ts_time" you need to invoke perform() and then to click on the element within it you can use either of the following locator strategies:
time_menu_button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li#ts_time")))
ActionChains(driver).move_to_element(time_menu_button).peform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#ts_time > a"))).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

How to locate an element inside this iframe with selenium?

I am tring to access an element inside this iframe:
I tried to use switch_to.frame(0) first, but still can not locate the element inside the frame.
Screenshot of the error:
As the element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='To Do Assignments in Connect'][src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='To Do Assignments in Connect' and #src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
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
References
You can find a couple of relevant detailed discussions in:
How can I select a html element no matter what frame it is in in selenium?
You should switch to the frame:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

"Accept Cookies" popup with Selenium in Python using explicit wait

I have been trying to webscrape some information from a webside with Selenium in Python. I load the webside, but I can't accept the cookies, which is necessary to continue. I have tried to use explicitly wait as I suspected that the problem was that the webside loaded before the "accept" bottom becomes clickable.
My code is:
import os
import until as until
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
os.environ['PATH'] += r"C:\Users\BackUp HDL\AppData\Local\Programs"
driver = webdriver.Chrome()
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="notice"]/div[3]/button[2]')))
driver.find_element_by_xpath('//*[#id="notice"]/div[3]/button[2]').click()
Whenever I try to run the code I get an error. Can someone please help me out?
The element Accepter is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accepter']"))).click()
Using XPATH:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Accepter']"))).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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to click pop up window button within a iframe using Selenium with Pyhton

I want to click the "Akkoord" button but I am unable to do that. I already tried different methods but they are not working. Any help will be appreciated.
one of the codes I tried.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.demorgen.be/nieuws')
time.sleep(20)
driver.find_elements_by_class_name('message-component message-button no-children pg-accept-button')[0].click()
The element Akkoord is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.demorgen.be/nieuws")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Akkoord']"))).click()
Using XPATH:
driver.get("https://www.demorgen.be/nieuws")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'sp_message_iframe')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Akkoord']"))).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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
use xpath or css
css :
[class="message-component message-button no-children pg-accept-button"]
driver.find_elements_by_css_selector('[class="message-component message-button no-children pg-accept-button"]')[0].click()
xpath:
//*[#class="message-component message-button no-children pg-accept-button"]
driver.find_elements_by_xpath('//*[#class="message-component message-button no-children pg-accept-button"]')[0].click()
find_elements_by_class_name expects single class name as argument thats why its not working as space in class indicates multiple classes.
THe find by class actually uses css under the hood. So if you want to find element having multiple class . You can replace space with '.' (THis works only in python)
driver.find_elements_by_class_name('message-component.message-button.no-children.pg-accept-button')[0].click()
Also
The element is inside iframe
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
frame = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#sp_message_container_404503 iframe")))
driver.switch_to_frame(frame)
driver.find_element_by_css_selector(
'[class="message-component message-button no-children pg-accept-button"]').click()

How to locate the button element using Selenium through Python

I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.
HTML:
To locate and click() on the element with text as Not Now you can use the following Locator Strategy:
Using xpath:
driver.find_element_by_xpath("//button[text()='Not Now']").click()
However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).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
Reference
You can find a couple of relevant discussions in:
What does contains(., 'some text') refers to within xpath used in Selenium
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text

Categories