Managing the new window selenium - python

I want to write a code to login to the "Origin" platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.binary_location = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.origin.com/irl/en-us/store")
time.sleep(5)
Menu_Button = driver.find_element_by_xpath("//*[#id='shell']/section/div/nav/div/div[1]/div[2]").click()
Sign_in_Button = driver.find_element_by_xpath("//div[#class='origin-cta-primary origin-telemetry-cta-primary']").click()
time.sleep(10)
Email_Adress = driver.find_element_by_xpath("//input[#name='email']").send_keys("Deneme")
Password = driver.find_element_by_xpath("//input[#name='password']").send_keys("Deneme123")
Login_Button = driver.find_element_by_xpath("//a[#id='logInBtn']").click()
Pressing the "Login" button opens a new window to enter the id password, but I cannot manage it

You can access new windows with driver.window_handles which is just a list with handles to all the driver's current windows. If the driver has only the main window and the login in window, the login window would be driver.window_handles[1].
You can switch the driver to this window with
driver.switch_to.window(driver.window_handles[1])
After that you should be able to process your code
Email_Adress = driver.find_element_by_xpath("//input[#name='email']").send_keys("Deneme")
....

I remember reading about this but I can't quite remember, this might point you in the right direction
https://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames
browser=webdriver.Firefox()
[...]
browser.switch_to_window(browser.window_handles[1])
browser.switch_to_window(browser.window_handles[0])

Related

Selenium driver hanging on OS alert

I'm using Selenium in Python (3.11) with a Firefox (107) driver.
With the driver I navigate to a page which, after several actions, triggers an OS alert (prompting me to launch a program). When this alert pops up, the driver hangs, and only once it is closed manually does my script continue to run.
I have tried driver.quit(), as well as using
os.system("taskkill /F /pid " + str(process.ProcessId))
with the driver's PID, with no luck.
I have managed to prevent the pop-up from popping up with
options.set_preference("security.external_protocol_requires_permission", False)
but the code still hangs the same way at the point where the popup would have popped up.
I don't care whether the program launches or not, I just need my code to not require human intervention at this key point.
here is a minimal example of what I currently have:
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options.set_preference("security.external_protocol_requires_permission", False)
driver = webdriver.Firefox(options=options)
# Go to the page
driver.get(url)
user_field = driver.find_element("id", "UserName")
user_field.send_keys(username)
pass_field = driver.find_element("id", "Password")
pass_field.send_keys(password)
pass_field.send_keys(Keys.ENTER)
#this is the point where the pop up appears
reqs = driver.requests
print("Success!")
driver.quit()
There are some prefs you can try
profile = webdriver.FirefoxProfile()
profile.set_preference('dom.push.enabled', False)
# or
profile = webdriver.FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', False)
profile.set_preference('dom.webnotifications.serviceworker.enabled', False)
Have you tried setting this preference to prevent the particular popup:
profile.set_preference('browser.helperApps.neverAsk.openFile', 'typeOfFile')
# e.g. profile.set_preference('browser.helperApps.neverAsk.openFile', 'application/xml,application/octet-stream')
Or have you tried just dismissing the popup:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
....
pass_field.send_keys(Keys.ENTER)
#this is the point where the pop up appears
WebDriverWait(driver, 5).until(EC.alert_is_present).dismiss()
reqs = driver.requests
...
check this checkbox manually then open the app for every app associated to the links you use, then it will work normally.

Is there a way to disable the end user from entering URL's with selenium in Python?

The goal is a locked-in end system.
from selenium import webdriver
driver = webdriver.Firefox()
driver.fullscreen_window()
driver.get("https://google.com/")
This opens a fullscreen window but still allows users to enter URL's by hovering the mouse near the top of the screen. Is there a way to permanently hide the URL bar? Alternatively, is there a way to disable entering a URL?
Kiosk mode will disable all GUI elements.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
option = Options()
option.add_argument("--kiosk")
driver = webdriver.Firefox(options=option)
driver.get("https://google.com/")

How To Block Chrome Window Popup in Selenium Python

I have a Selenium-Python script for performing some automation tests on website. The script repeatedly opens some new tabs performs some work on the opened window and closes it.
Issue I'm facing is that whenever a new tab is opened my chrome window pops up from Minimize state to maximize. I want it to do all the task in background without interuppting.
Ps: Headless version is not applicable for my scenario.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options,executable_path="chromedriver.exe")
driver.get("https://xyx.org/#/login") #Login manually to a website.
while 1:
#some stuff here
main_window = driver.current_window_handle
driver.execute_script("window.open();")
driver.switch_to.window(driver.window_handles[1])
driver.get("some link here ")
#doing some work here
driver.close()
driver.switch_to.window(main_window)
If I minimize the chrome window manually then whenever the driver.execute_script("window.open();") is executed it automatically maximizes the chrome window. I want it to just keep remain minimized and do the work.
My fixed solution, using two driver instead of one. Driver one to login and driver two to do the work on the window with headless mode.
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.headless = True
driver_one = webdriver.Chrome(executable_path=r'/chromedriver')
driver_one.maximize_window()
driver_one.get("https://xyx.org/#/login") #Login manually to a website.
while 1:
#some stuff here
driver_two = webdriver.Chrome(executable_path=r'/chromedriver',
options=options)
link = "https://www.google.com"
driver_two.get("some link here ")
#doing some work here
driver_two.close()

How to get selenium to move the slider in google extensions settings

I'm trying to make an Instagram bot and it somewhat works but I want it to be able to post pictures, the extension I'm using blocks the page after a few seconds of it being loaded. I found a thread somewhere and someone said if you go into incognito mode it fixes this problem. The problem is I don't know how to make the move the slider with XPath.
Code:
from selenium import webdriver
from time import sleep
from info import pw
from info import email
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension('mhh.crx.crx')
chrome_options.add_argument("--disable-extension")
class InstaBot:
def __init__(self, username, pw):
self.username = username
self.driver = webdriver.Chrome("C:/path stuff/chromedriver.exe", chrome_options=chrome_options)
sleep(1)
self.driver.get("chrome://extensions/?id=jkcghkeldalkgfhgenmcblejihijdpha")
sleep(1)
self.driver.find_element_by_xpath("//a[contains(text(), 'crToggle')]")\
.click()
#sleep(2)
self.driver.get("chrome-extension://odlpjhnipdekfkdkadoecooboghijleh/app/index.html")
sleep(2)
self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\
.click()
my_bot = InstaBot('tonka_r_us', pw)
pictures:
https://imgur.com/a/FLfrFxq
To open your Chrome webdriver in incognito mode, best way is to use Options.
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome('..\drivers\chromedriver',options=options)
driver.get("https://stackoverflow.com/")

Selenium chrome Driver upload file and then submit

file and then click the submit button
for some reason after i run the python script .It justs opens the upload window
and sits there
here is my code .I am using python on windows 10
from selenium import webdriver
driver =
webdriver.Chrome(executable_path='C:\chromedriver\chromedriver.exe')
driver.get('http://localhost:5000/upload')
element =
driver.find_element_by_id("uploadfile").send_keys("c:\\projects\\input.xml")
modified code --working
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver =
webdriver.Chrome(executable_path='C:\chromedriver\chromedriver.exe')
driver.get('http://localhost:5000/uploadxml')
element =
driver.find_element_by_id("uploadfile")
.send_keys("c:\\projects\\inout.xml")
click = driver.find_element_by_id("submitfile")
click.click()
You only select the element. But you don't klick the submit.
Take this code snippet after your select.
click = driver.find_element_by_id("**id**")
click.click()

Categories

Resources