Facing challenge with selenium webdriver on Win 10 system - python

I am trying to automate login on a website using selenium. (Windows 10 64 bit os)
Using the below code I am able to open a link,
But once webpage loads the get cmd does not release my python interpreter
next cmd:
browser.find_element_by_class_name('gb_g').click()
does not run.
I tried to open google.com but the issue is same, I tried different browser it works but my project URL only works with internet explorer.
I have tried with both 64 & 32-bit driver version of internet explorer
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
link = 'https://www.google.com/'
browser = webdriver.Ie(executable_path='S:\work\Automation\Elog\IEDriverServer.exe')
browser.get(link)
browser.find_element_by_class_name('gb_g').click()

You need to consider a few things:
If your use case is to use internet-explorer you need to use IEDriverServer.exe through selenium.webdriver.ie.webdriver.WebDriver() Class as follows:
webdriver.Ie(executable_path=r'S:\path\to\IEDriverServer.exe')
To click() on the element with text as Gmail you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
from selenium import webdriver
browser = webdriver.Ie(executable_path=r'S:\work\Automation\Elog\IEDriverServer.exe')
browser.get("https://www.google.com/")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail"))).click()
Using CSS_SELECTOR:
from selenium import webdriver
browser = webdriver.Ie(executable_path=r'S:\work\Automation\Elog\IEDriverServer.exe')
browser.get("https://www.google.com/")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.gb_g[href*='com/mail/?tab']"))).click()
Using XPATH:
from selenium import webdriver
browser = webdriver.Ie(executable_path=r'S:\work\Automation\Elog\IEDriverServer.exe')
browser.get("https://www.google.com/")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Gmail']"))).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
Update (from #rahuldesai's comment)
With the above mentioned configurations and using the 32 bit IEDriverServer binary some how the issue gets fixed.

Related

Prevent Selenium from taking the focus to the opened window

I have 40 Python unit tests and each of them open a Selenium driver as they are separate files and cannot share the same driver.
from selenium import webdriver
webdriver.Firefox()
The above commands will take the focus to the new opened window. For example, if I am on my editor and typing something, in the middle of my work, suddenly a selenium browser is opening and Linux switch to that window. I am not sure if Windows or Mac have a similar problem or not.
This means that every time I run a unit, I cannot use my computer as it keeps switching away from the application that I am currently using.
How can I tell Selenium not to switch to the opened window?
Here is an example of running Selenium/Firefox on linux, in headless mode. You can see various imports as well - gonna leave them there. Browser will start in headless mode, will go to ddg page and print out the page source, then quit.
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
firefox_options = Firefox_Options()
firefox_options.add_argument("--width=1280")
firefox_options.add_argument("--height=720")
firefox_options.headless = True
driverService = Service('chromedriver/geckodriver') ## path where geckodriver is
browser = webdriver.Firefox(service=driverService, options=firefox_options)
wait = WebDriverWait(browser, 20)
browser.get('https://duckduckgo.com')
print(browser.page_source)
browser.quit()
Browser can also take screenshots while in headless mode, if any unit test requires it.
A reasonably well structured documentation for Selenium can be found at https://www.selenium.dev/documentation/ (with some gaps, of course, but generally decent).
You can run your Selenium drivers in headless mode. This will prevent moving focus on opened by Selenium browsers and will not disturb you working on your PC machine.

How do I enter a type command onto my selenium script and also turn it into headless mode?

I am working with the following selenium script:
from selenium import webdriver
PATH= r"C:\Users\Hamid\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.google.com/")
cookie_button=driver.find_element_by_xpath('//*[#id="L2AGLb"]/div').click()
How do I add a command line to enter 'ONS data' onto the google search tab? Also, how can I turn the window into headless mode?
You can send the character sequence to the Google Search area using the following locator strategy:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
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
But once you have initiated google-chrome in headed mode you won't be able to shift to google-chrome-headless mode within the same session.
You can find a relevant detailed discussion in How can I switch from headless mode to normal mode using Google Chrome and Selenium?

Why am I getting the message that the search-bar is not intractable?

The code is supposed to type "fish" into the YouTube search bar using Selenium and a Chrome Browser.
I have tried the xpaths of mulitple divs that hold the tag and they didn't work either.(not sure if the error was the same though) The xpath in the code is for the <input> tag so it should be fine.
I also watched a tutorial and the xpath was exactly the same so that shouldn't be the problem since it worked for the YouTuber.
It also took me some time to figure out that the find_element_by_* are depreciated functions.
Could it be that the .send_keys has also been changed? I did try to find the selenium changes in 4.1.0 and it said nothing about it on a website that I found.
Should I maybe delete Selenium 4.1.0 and install an older version? For simplicity sake. Since there is probably a bigger number of tutorials for it.
from selenium import webdriver
from selenium.webdriver.common.by import By
setting = webdriver.ChromeOptions()
setting.add_argument("--incognito")
# I open the browser in incognito just so I don't clutter my search
# history with dumb stuff as I'm testing things out
# could it be a part of the problem?
driver = webdriver.Chrome(options = setting)
driver.get('http://youtube.com')
searchbox = driver.find_element(By.XPATH, '//*[#id="search"]')
searchbox.send_keys('fish')
Error Message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
wait=WebDriverWait(driver,60)
driver.get('http://youtube.com')
searchbox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#search")))
searchbox.send_keys('fish')
In order to send_keys to that element wait for it to interactable and then send keys.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Outputs:

Python Selenium to Scrape USPS

I am trying to create a script to login to USPS website to get a list of incoming packages from Informed Delivery.
I have tried two methods:
Requests
Selenium
Requests
I captured the Login request and imported into Postman. When I sent request, I received error:
{
"actionErrors": [
"We have encountered an error. Please refresh the page and try again."
],
"actionMessages": [],
"fieldErrors": {}
}
In the request body, it sends a token value (from login form). The request headers also send a few headers starting with x-jfuguzwb-. These look to be tokens of different values.
Selenium
Even using a headless browser didn't work.
LOGIN_URL = "https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/"
driver.get(LOGIN_URL)
username = driver.find_element_by_name('username')
username.send_keys(USERNAME)
password = driver.find_element_by_name('password')
password.send_keys(PASSWORD)
driver.find_element_by_id('btn-submit').click()
displays an error saying "Our apologies that you are having issues with your login."
There was a Python Module called myusps but it has not been updated for a couple years.
Are there any suggestions as to how I can accomplish this?
This answer below has helped me solve automation issues with site logins that shall go unnamed. I recommend taking a look at the user #colossatr0n answer.
You can use vim, or as #Vic Seedoubleyew has pointed out in the answer by #Erti-Chris Eelmaa, perl, to replace the cdc_ variable in chromedriver(See post by #Erti-Chris Eelmaa to learn more about that variable). Using vim or perl prevents you from having to recompile source code or use a hex-editor. Make sure to make a copy of the original chromedriver before attempting to edit it. Also, the methods below were tested on chromedriver version 2.41.578706.
Can a website detect when you are using selenium with chromedriver?
A bit of more information about your usecase and the error Our apologies that you are having issues with your login
which you are seeing would have helped us to debug the issue in a better way. However, I was able to send a character sequence to both the username and password field and invoke click() on the Sign In button using Selenium inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using css-selectors:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("Bijan")
driver.find_element_by_css_selector("input#password").send_keys("Bijan")
driver.find_element_by_css_selector("button#btn-submit").click()
Using xpath:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://reg.usps.com/entreg/LoginAction_input?app=Phoenix&appURL=https://www.usps.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='username']"))).send_keys("Bijan")
driver.find_element_by_xpath("//input[#id='password']").send_keys("Bijan")
driver.find_element_by_xpath("//button[#id='btn-submit']").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:

Cannot select element on page with any kind of selector with python selenium

Im trying to simply fill login form, but no matter how much i try I just cannot. I'm trying for two days with all kinds of selectors, nothing. Here is my code:
# -*- coding: iso-8859-2 -*-
from __future__ import print_function
import pyautogui, sys
import time
import random
import subprocess
import os
import urllib
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data") #Path to your chrome profile
options.add_argument("disable-infobars")
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
driver.get("https://awario.com/login?r=%2F")
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='loginform-email']"))).click
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='loginform-email']"))).send_keys('email')
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='loginform-password']"))).send_keys('pass')
recommend you use selenium server way for run and debug script on local as guide: http://selenium-python.readthedocs.io/getting-started.html#using-selenium-with-remote-webdriver
The console window running selenium server will print out log of all operation on browser as below:
INFO - Executing: [find elements: By.cssSelector: a[ng-if*="uxdRegion"]])
INFO - Done: [find elements: By.cssSelector: a[ng-if*="uxdRegion"]]
INFO - Executing: [get element attribute: 0 [
css selector: a[ng-if*="uxdRegion"]], href])
INFO - Done: [get element attribute: 0 [] ->
css selector: a[ng-if*="uxdRegion"]], href]
And stop running and print out log when meet exception.
From the log you can easy to know which step in script fail and failed reason.
FYI, the way of using selenium in your above script, called 'directconnect'.
I tried your xpath "//*[#id='loginform-email']" on your login page on my laptop with normal browser size, it will find 3 matched elements.
The first and the second one not visiable, they used for small browser size, like
open your site on mobile device. only the third one visible in my trying.
For selenium not visible element is always not clickable.
In your script you used the api: EC.element_to_be_clickable(), because there are more than one matched element, it will use the first one which is not visible,
equivalent to not clickable, so EC.element_to_be_clickable() should not return
any element for next click.
you can try with more strict locator as below code on desktop with maixmize browser size to verify my answer correct or not.
css_locator_email = ".top-index-section + div #loginform-email"
wait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, css_locator_email))).click();

Categories