Switch to another popup window in selenium - python - python

I want to Switch to another window to close it however i am unable to do it. I have added explicit wait so that the close button can appear and then i can close it
I thought it must be iframe but it is not need a help on this
Below is the screen shot and code for it
The main code which i have implemented is on the last line
enter image description here
Below is code
from selenium.webdriver.chrome.webdriver import WebDriver
from utilities.BaseClass import BaseClass
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\chromedriver")
driver.get("https://www.seleniumeasy.com/test/")
driver.maximize_window()
time.sleep(5)
driver.find_element_by_xpath("//a[text()='No, thanks!']").click()
driver.find_element_by_link_text("Progress Bars & Sliders").click
driver.execute_script("window.scrollTo(300, 500)")
driver.find_element_by_link_text("Progress Bars & Sliders").click()
driver.find_element_by_link_text("JQuery Download Progress bars").click()
driver.find_element_by_xpath("//button[#id='downloadButton']").click()
wait = WebDriverWait(driver,20)
wait.until(ec.element_to_be_clickable((By.XPATH,"//button[#type='Close']")))
driver.find_element_by_xpath("//button[#type='Close']").click()

You are using a wrong locator //button[#type='Close'] here:
wait.until(ec.element_to_be_clickable((By.XPATH,"//button[#type='Close']")))
driver.find_element_by_xpath("//button[#type='Close']").click()
It should be //button[text()='Close'].
So instead of the above try this:
wait.until(ec.element_to_be_clickable((By.XPATH,"//button[text()='Close']"))).click()
The entire code will be:
from selenium.webdriver.chrome.webdriver import WebDriver
from utilities.BaseClass import BaseClass
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\chromedriver")
driver.get("https://www.seleniumeasy.com/test/")
driver.maximize_window()
time.sleep(5)
driver.find_element_by_xpath("//a[text()='No, thanks!']").click()
driver.find_element_by_link_text("Progress Bars & Sliders").click
driver.execute_script("window.scrollTo(300, 500)")
driver.find_element_by_link_text("Progress Bars & Sliders").click()
driver.find_element_by_link_text("JQuery Download Progress bars").click()
driver.find_element_by_xpath("//button[#id='downloadButton']").click()
wait = WebDriverWait(driver,20)
wait.until(ec.element_to_be_clickable((By.XPATH,"//button[text()='Close']"))).click()

Related

Cant click or send keys, selenium webdriver python

I am trying to access the following website:
and apply some filters like click on and select a date. When I click on I want to select one of the two options. But I cant click with xpath neither can I send keys to to type in what I want. Can someone help me by finding how I can click on and right after that select one of the two options and click the green button so that I can click afterwards on the date?
here is what I got so far in code (Python)
%pip install selenium webdriver_manager
import requests
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get(url_dist_vacinas)
print(driver.title)
driver.find_element(By.XPATH,'//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
Try the below lines of code, this might help
driver.get("https://infoms.saude.gov.br/extensions/DEMAS_C19VAC_Distr/DEMAS_C19VAC_Distr.html")
clickReviw = WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Tipo de vacina']")))
clickReviw.click()
sleep(4)
driver.find_element_by_xpath("//input[#placeholder='Search in listbox']").send_keys("vacina")
btn1 = driver.find_element_by_xpath("(//div[#class='qv-listbox-text qv-listbox-text-value'])[1]")
btn1.click()
btn2 = driver.find_element_by_xpath("//button[#title='Confirm selection']")
btn2.click()
Imports
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
from time import sleep

Two browser pages opened when using selenium on iframe on Streamlit

I can't execute selenium code on the iframe page on streamlit. When I run the .py file by executing streamlit run app5.py on cmd, it generates two browsers with one being the streamlit app (localhost:8501) and the other one being an empty page with an alert message on the top saying "Chrome is being controlled by automated test software." My guess for the reason why selenium code is not running is because it's executing selenium on the empty page not on the streamlit page. Is there a way to remove or not populate the empty page?
Here's my code:
import streamlit as st
from dateutil.parser import parse
import streamlit.components.v1 as components
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
import time
st.title("Auto Search App")
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
components.iframe("https://wego.here.com/", height = 500)
search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)
Here is the screenshot of the two pages (empty page is generated most recently) when I execute streamlit run app5.py on cmd:
Run selenium headless.
Code:
import streamlit as st
from dateutil.parser import parse
import streamlit.components.v1 as components
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.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 20)
components.iframe("https://wego.here.com/", height = 500)
search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)
You can do it by using window_handles and switch_to_window method.
Before clicking the iframe first store the window handle
See if it works
import streamlit as st
from dateutil.parser import parse
import streamlit.components.v1 as components
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
import time
st.title("Auto Search App")
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
#Switch to the other window
window_before = driver.window_handles[0]
driver.switch_to_window(window_before)
components.iframe("https://wego.here.com/", height = 500)
search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)

Close pop-up window on website using Selenium

So I'm trying to scrape some information from a website and can't get through a pop-up window. I've tried using short and full Xpath of the X button but it doesn't close.
here is my code
# import
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.get('https://ai.fmcsa.dot.gov/SMS')
driver.find_Element_By_xpath('//*[#id="simplemodal-container"]/a').click();
The code does open the website but doesn't close the pop-up. What might be the issue?
You automation script needs an explicit waits, and the below xpath :-
//a[#title='Close']
Code : -
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Close']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
If you want to use your code as is:
# import
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.get('https://ai.fmcsa.dot.gov/SMS')
driver.find_Element_By_xpath('/html[1]/body[1]/div[7]/a[1]').click();
This worked for me and closed the pop up window.

element not clickable from dropmenu

using selenium, i try de choose a specific value in a drop menu,
but i have always an error
%reset -sf
site = 'https://www.mytek.tn/informatique/ordinateurs-portables/pc-portable.html'
driver.get(site)
sleep(1)
page_cat = requests.get(site)
tree_cat = html.fromstring(driver.page_source)
btn_all = tree_cat.xpath(".//option[#value='all']")
if len(btn_all) == 0:
print("btn all dont exist")
else:
print('choice all exist')
dropdown = Select(driver.find_element_by_id('limiter'))
dropdown.select_by_visible_text('Tous')
#dropdown.select_by_value('all') # same error : ElementNotInteractableException
i've tried de see if selenium can read all the element in the drop menu : yes
print("All selected options using ActionChains : \n")
for opt in dropdown.options:
print(opt.get_attribute('innerText'))
time.sleep(5)
always same error
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
I'm going crazy
my imports:
#imports here
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import requests
import time
from time import sleep
from lxml import html
import logging as log
import pandas as pd
The drop down menu you trying to access appearing on the bottom of the page, not inside the initially visible screen.
To access it with Selenium you need first to scroll to that element.
Also, there are 2 selects there with similar locators while you need the second of them, so you should use corrected locator
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
actions = ActionChains(driver)
wait = WebDriverWait(driver, 20)
dropdown = wait.until(EC.presence_of_element_located((By.XPATH, "(//select[#id='limiter'])[last()]")))
time.sleep(1)
actions.move_to_element(dropdown).perform()
time.sleep(0.5)
dropdown = Select(driver.find_element_by_xpath("(//select[#id='limiter'])[last()]"))
dropdown.select_by_visible_text('Tous')
I hope this will work for you.

Selenium: Why isn't a button click carried out?

I would like to click the "Search" button on a website. Then the next page opens. The click is not carried out (not even with built-in time.sleeps(3)), but an error message is not thrown.
What am I doing wrong?
import time
import sys
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('G:\\chromedriver\\chromedriver.exe')
driver.get("https://www.immobilienscout24.de")
driver.execute_script("return document.readyState") == "complete"
element = driver.find_element_by_id("oss-location")
#element.clear()
element.send_keys("10115 Berlin")
btn=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='oss-main-criterion oss-button button-primary one-whole']")))
#btn.click()#trial 1
#driver.execute_script("arguments[0].click();", btn) #trial 2
print("ready")
I would suggest after inserted the value in search box wait for auto search result and then click the auto search result.Then click on the treffer button.
Code:
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('G:\\chromedriver\\chromedriver.exe')
driver.get("https://www.immobilienscout24.de")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"oss-location"))).send_keys("10115 Berlin")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//li[#class='ui-menu-item'][contains(.,'10115 Berlin')])[1]"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='oss-main-criterion oss-button button-primary one-whole']//span[contains(.,'Treffer')]"))).click()

Categories