I try to set up a test for a webpage using Selenium WebDriver and Python. Therefore I start the Docker image selenium/standalone-firefox.
Within this test normally a JavaScript written prompt pops up and want to receive an entry prior I can click OK.
But how can I interact with this prompt and the OK button?
On Selenium IDE the recorder uses answer on next prompt for that. How to do this with Python-Selenium? If Python does not support an corresponding command, how do I get the needed information to do the same with the available commands?
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.firefox.options import Options
# connect to docker Selenium Server
options = Options()
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=options.to_capabilities()
)
driver.get("https://www.ecalc.ch/motorcalc.php?hacker&lang=en&weight=4500&calc=auw&motornumber=1&warea=60&elevation=300&airtemp=25&motor=hacker&type=2|a60-7xs_v4_28-pole&gear=1&propeller=apc_electric&diameter=18&pitch=10.0&blades=2&batteries=topfuel_light_4500mah_-_30/45c&s=8&esc=master_spin_160_pro&cooling=good")
print(driver.title)
driver.find_element_by_id("modalConfirmOk").click()
driver.find_element_by_name("btnCalculate").click()
driver.find_element_by_id("AddCSV").click()
????
You must handle the prompt alert. Try it with:
driver.switchTo().alert().sendKeys("Your project name");
You can handle using alert class in selenium.
#Switch the control to the Alert window
obj = driver.switch_to.alert
time.sleep(2)
#Enter text into the Alert using send_keys()
obj.send_keys('test')
refer this link selenium
Related
I am trying to send a ALT+ESC command to my selenium chrome-driver to send it to the back of all other windows
This is the relevant code
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_ALT, Keys.ESCAPE)
actions.perform()
this is not working please help
To press key-combo:
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).key_down(Keys.LEFT_ALT).send_keys(Keys.ESCAPE).key_up(Keys.LEFT_ALT).perform()
But seems this is an OS-level keys combination for work with windows and this will not work in the selenium context.
Selenium actions applied to web page elements, fire some events inside browser.
ultimately I couldn't find a way to execute a OS command in selenium.
The following code has the same functionality but dose not necessarily run on the browser
from pyautogui import hotkey
hotkey('altleft', 'esc')
I'm trying to install chrome extention with selenium but when I clicked on "Add Extension", there was an alert like the picture below showed up, I'm not sure if it is an alert or not , but I saw error "no such alert". please help me
this is how I tried to handle this
alert = driver.switch_to.alert
alert.accept()
P/s : I also tried some other ways of handling alert but it wasn't worked so I don't think it is an alert. And I also don't want to use add_extension option
EDITED
Well, if you want to click on the alert message, try using this
WebDriverWait(driver, wait_time).until(EC.alert_is_present())
alert_msg = driver.switch_to.alert
alert_msg.accept()
For some reason I was only able to click on the alert when I used the WebDriverWait to wait until the alert is present.
Workaround
If you want to add a extension you should use the options argument.
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_extension('path/to/extension.crx')
driver = webdriver.Chrome('path/to/chromedriver.exe', options=options)
To download the CRX file use this extension: link
workaround number 2
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, wait
from time import sleep
import pyautogui
driver = webdriver.Chrome(r'.\drivers\chromedriver.exe')
driver.get('https://chrome.google.com/webstore/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa')
elem = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/div/div[2]/div[2]/div/div')))
elem.click()
sleep(3)
pyautogui.hotkey('tab','enter', interval=0.1)
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Checkout" button using "element_to_be_clickable((By.XPATH".
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket
And this is my current code for the clicking:
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.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
checkout = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH("//img[contains(#src,'/static/images/checkoutv2.png.png')]"))).click()
I can see your xpath is not correct.
Your Xpath should be.
//img[contains(#src,'/static/images/checkoutv2.png')]
Your code should be.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
The link you provided contains hCaptcha, which is actually responsible to check whether you are a human being or a bot. I guess that it's also the reason, why you can't click any of the items on the page, because Selenium actually is nothing less than a bot.
You first have to pass the test by clicking on the images, which are asked for.
I'm trying to log into paypal.com and make a payment automatically.
The program successfully loads the login page(https://www.paypal.com/us/signin) and inputs the email, but when I click the next button the web driver unexpectedly closes without generating an error message.
Has anyone encountered this issue before? Could it be because the next button is a disguised captcha, to keep robots from logging in?
I have already tried using time.sleep(3) to give the page time to load. I can't see any other issues with the code.
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
def paypal_pay(): # pass in user address
driver = webdriver.Chrome()
timeout = 20
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
def main():
paypal_pay()
main()
Your code is working fine but the way that you have implemented is causing the problem, I mean you are using the main() method and what it will do is, once this method gets called and executed, it will close all the connections at end. Hence the reason your browser also getting closed without any error because your code is working fine till there:
Try the below modified code without main method which works completely fine :
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe");
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "hello#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
print("=> Done...")
For more info on main(), refer this link
I hope it helps...
When I run your code After click on next button the Chrome browser crashes and in console i can see the following error.
<selenium.webdriver.remote.webelement.WebElement (session="577ff51b46a27eefeda43ccd320db48b", element="0.571535141628553-1")>
That means you need to start a RemoteWebDriver instead of ChromeDriver.
Step 1: Download the Selenium Standalone Server from following link
https://www.seleniumhq.org/download/
Step 2: open command prompt as an administrator go to the downloaded path and type the below command and press enter
java -jar selenium-server-standalone-3.141.59.jar
Step 3: To verify Hub is running, open a browser and type the below url.Default port of hub is 4444
http://localhost:4444/grid/console
Step 4: Use the following code.If you follow above steps properly it should work perfectly the below code.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def paypal_pay(): # pass in user address
desired_caps = DesiredCapabilities.CHROME
grid_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=grid_url)
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
nextElement = driver.find_element_by_id('btnNext')
nextElement.click()
def main():
paypal_pay()
main()
Please let me know if this work for you.Good Luck.
I am using the below code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.select import Select
import os
import win32com.client as win32
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://itsm.windstream.com/')
shell = win32.Dispatch("WScript.Shell")
time.sleep(5)
shell.Sendkeys('My_id')
shell.Sendkeys('{TAB}')
shell.Sendkeys('My_password')
shell.Sendkeys('{ENTER}')
Once i open the link, Chrome will pop up asking the id and password.
I am using shell and it was working previously.
Now this is not working.
Getting console output as (chrome Console)
text.cc Not Implemented
In python shell, No errors shows up.
Please assist.
Thanks.
I tried everything available in stack over flow and it doenst work
I solved this by adding a new line.
window_before = driver.window_handles[0]
driver.switch_to_window(window_before)
Even though the driver is in the current frame, the new update of chrome driver doesn't recognize. After switching to current window, the code works.
Thanks for helping.
If it's kind of alert type, you can use Alert objects
alert = driver.switchTo().alert()
alert = wait.until(alertIsPresent())
and then
alert.getText()
alert.sendKeys()
aler.accept()
alert.dismiss()
Try to pass authentication while you get the url, something like this
driver.get('http://admin:admin#itsm.windstream.com');