below is part of a code i am using. It works fine except for when firefox opens it only takes up half the screen, and towards the bottom left of the screen. is there a way of altering the size of firefox when it opens?
browser = webdriver.Firefox()
#enter website into searchbox
browser.get('https://website')
#find the password element in page source
inputElement = browser.find_element_by_id("password")
#input users password into website
inputElement.send_keys(password)
inputElement.send_keys(Keys.RETURN)
Maximize (Full screen) the Firefox window as below:
browser.maximize_window()
Related
I'm pretty new to selenium and i searched a lot, but can't find an answer to my problem.
I want to open firefox, go on google and search for something. Store everthing in a list and print it to my console.
But everytime i open firefox with selenium, a pop-up windows opens with a confirmation to the privacy regulations of google. I dont know how to close it.
Here is what i tried:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("http://www.google.com")
main_page = driver.current_window_handle
time.sleep(3)
#Tried to find out in which browers window I am right now -> terminal says '19'
print('The actual browers window is: {}'.format(main_page))
driver.find_element_by_xpath("//*[#id='introAgreeButton']/span/span").click()
# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("Flowers")
search_field.submit()
# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")
# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")
# iterate through each element and print the text that is
# name of the search
i=0
for listitem in lists:
print (listitem.get_attribute("innerHTML"))
i=i+1
if(i>10):
break
# close the browser window
driver.quit()
Google privacy popup is contained in iframe. You must switch to iframe and then look for accept (or deny) button. Like so
wait = WebDriverWait(driver, 5)
iframe = wait.until(EC.element_to_be_clickable([By.CSS_SELECTOR, '#cnsw > iframe']))
driver.switch_to.frame(iframe)
And then when in iframe, look for Accept button a click it.
I would like to have a URL open in a new tab and closing itself rather than opening in front of the user as it currently stands. I want it to be a discrete as possible.
Here is the python code I have so far that works, but brings the window to the front:
def extract(self, id, pass):
chrome_dir_path = '/Users/<user>/Downloads/chromedriver'
driver = webdriver.Chrome(chrome_dir_path)
driver.implicitly_wait(5)
driver.maximize_window()
driver.get('https://<URL>')
username = driver.find_element_by_id('user')
username.send_keys(USER)
password = driver.find_element_by_id('password')
password.send_keys(PASS)
driver.find_element_by_name('remUID').click()
python_button = driver.find_element_by_class_name('button')
python_button.click()
running headless resolved this issue.
I'm trying to login to my yahoo account via selenium. I'm simply learning at this point and have made programs for different websites in order to get more comfortable with Selenium overall.
I'm now trying to login to Yahoo and I haven't been able to figure it out.
URL: https://login.yahoo.com/
I've tried doing:
yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "#login-username"))).sendKeys("tester#yahoo.com")
yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "login-username"))).sendKeys("tester#yahoo.com")
yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//input[#id='login-username']"))).sendKeys("tester#yahoo.com")
yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/form[1]/div[2]/input[1]"))).sendKeys("tester#yahoo.com")
Anything that I'm particularly missing? Would appreciate if someone told me how to get this login to work. Rather have that than a simple copy paste of code :) Thank you!
I've done some further testing and it works when on the main tab. However, I am opening a new tab with the yahoo login page it doesn't work. Do I have to do something differently to type on a new tab?
I used another approach without WebDriverWait to solve, hope it helps.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--window-size=1920x1080")
browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
# Opening yahoo page in a new tab
browser.execute_script("window.open('https://login.yahoo.com/');")
# Switch to new tab
browser.switch_to.window(browser.window_handles[-1])
# Selecting login-username and putting email
browser.find_element_by_id('login-username').send_keys('tester#yahoo.com')
Result:
Here is the sample script.
url = "https://login.yahoo.com/"
# Step 1 -navigate to the AUT
driver.get(url)
print ("Step 1 - Done")
# Step 2 - Enter the username
#wait for the user name to be displayed
userName = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'input#login-username')))
userName.send_keys("used css")
# Step 3 - click on Next
driver.find_element_by_xpath("//input[#id='login-signin']").click()
# Step 4 - Enter password
passWord = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='password']")))
passWord.send_keys("password")
# Step 5 - Click on Sign in
driver.find_element_by_id("login-signin").click()
Here is how you can do the script development quick and effectively.
Keep a break point after browser navigated to url
Get the element xpath using chrome devtools, refer here to how to get test and get the xpath
Go to Console > click on show python prompt to open the interactive console
Enter the code here and hit enter to check if that line works when you place it in your script
Make any necessary change and confirm the step works
Copy paste the step from interactive console to script
I want to open several weblinks under a website in 1 browser (several tabs). The website requires login and password.
When login and password keyed in. it turns to a verification page, asks for the verification code sent to me by email.
I checked the email and key in verification code on the verification page. Login is successful.
The existing browser is in front of me.
However the codes are not picking it up, and open another tab as wanted. Seems a certain connection is lost.
How can I continue? (or as an alternative, how can Python to reuse the existing Chrome browser?)
The codes usually works well but comes to this case (login, enter verification code), it doesn't.
import os, time
from selenium.webdriver import ChromeOptions, Chrome
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "C:\\Python27\\Scripts\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
opts = ChromeOptions() # leave browser open after code
opts.add_experimental_option("detach", True) # leave browser open after code
opts.add_argument('disable-infobars')
driver = webdriver.Chrome(chromedriver, chrome_options=opts) # leave browser open after code
driver.maximize_window()
verificationErrors = []
accept_next_alert = True
time.sleep(5)
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.window_handles[0]
driver.switch_to_window(window_0)
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("password")
driver.find_element_by_id("Submit").click()
time.sleep(60)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
window_1 = driver.window_handles[1]
driver.switch_to_window(window_1)
time.sleep(3)
driver.get('https://anotherwebsite.com')
time.sleep(3)
sys.exit()
You can try below to perform some actions on two different pages/tabs:
# Handle base page
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.current_window_handle
...
# Handle new page
driver.execute_script('window.open("https://anotherwebsite.com");')
window_1 = [window for window in driver.window_handles if window != window_0][0]
driver.switch_to_window(window_1)
# driver.close() # To close new tab
...
# Switch back to base page
driver.switch_to_window(window_0)
I am working on the below script to open multiple website in new tabs. This is working fine in Firefox as expected.
But it is not working in Chrome. It opens a new tab as the second tab, but the second website link opens in the first tab itself. Later again a new tab is opened as the third tab, then the third link opens in the first tab itself. The commented part ( 5 th line ) is how I call the chromedriver.exe.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() #Chrome('C:\\data\\books oae\\apex library\\chromedriver') #Firefox()
browser.get('https://trello.com/login')
time.sleep(10)
emailElem = browser.find_element_by_id('user')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://todoist.com/Users/showLogin')
time.sleep(10)
emailElem = browser.find_element_by_id('email')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://asana.com/#login')
time.sleep(10)
emailElem = browser.find_element_by_id('login-email-login-modal')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('login-password-login-modal')
passwordElem.send_keys('test12345')
passwordElem.submit()
Splinter is the answer to your problem. It's a wrapper around Selenium that does all your Display-handling for you. Just by creating new Splinter objects, you open new windows. You can specify if you want them to be Chrome or Firefox.