Hello and happy Holidays!
I wrote a script where I open a webpage in chrome, login, go to a certain page and then trying to print that page.
When the Chrome print preview comes up, I can not target the print button neither try to just hit enter via automation. Nothing works.
Any ideas please?
Ps: This is a the code I wrote but the login information will be incorrect
Thank you in advance!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from shutil import which
import time
from pynput.keyboard import Key, Controller
keyboard = Controller()
chrome_path = which("chromedriver")
driver = webdriver.Chrome(executable_path="chromedriver")
name = '****'
password = '****'
driver.get(
"https://www.winbank.gr/sites/corporate/el/Pages/default.aspx")
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
username = driver.find_element_by_name('Username')
username.send_keys(name)
passwords = driver.find_element_by_name('Password')
passwords.send_keys(password)
passwords.send_keys(Keys.ENTER)
time.sleep(10)
driver.find_element_by_xpath(
'//*[#id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div[2]/div/div[2]/div/div/div[6]').click()
window_before = driver.window_handles[0]
time.sleep(6)
driver.find_element_by_xpath(
'//*[#id="ng-app"]/body/div[3]/div/div/div/div/div/div/div/div[3]/div[2]/wb-pfm-transactions/div/div[2]/div/div[22]/div[2]').click()
time.sleep(3)
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_id('print').click()
driver.switch_to.window(driver.window_handles[-1])
print('-------------test----------')
# driver.print = "function(){};"
keyboard.press(Key.enter)
driver.find_element_by_xpath(
'/html/body/print-preview-app//print-preview-sidebar//print-preview-button-strip//cr-button[2]').click()
Usually when dealing with untargetable objects I use the tab key to navigate and enter to "click" the element currently focused, what you should do is:
1. Try to print it manually, but with using tabs to navigate and Enter to click
2. Keep track of all key presses needed
3. Write your code accordingly
Edit: Also, if you need help with automatically pressing keys please refer to this link.
Related
I'm automating steps/click on a website using Python and Selenium.
I need a solution with the 'while true:' loop to exit when there is none more to click and to go to the next line of code.
I've been searching and tried everything I could but can't get this to work.
The web page I access and need automate has variable amount pages that changes daily.
At the bottom of each page there is a 'view more' button.
So far, the code I have does the job of scrolling down and clicking each button until there is no more to click.
Problem is the code exits on error when there are none more to click.
I will appreciate it if someone can help me with a solution please.
I'm a novice (but I'm trying) when it comes to Python(or any coding) so please be gentle and descriptive.
Thank you.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = "https://web_page_i_want_to_open.html"
username = 'abcdefgh'
password = '123456'
browser = webdriver.Firefox()
browser.get(url)
time.sleep(5)
#find and click username field and insert login name
browser.find_element_by_xpath('/path_to_login_name_field').send_keys(username)
#find and click password field and insert password
browser.find_element_by_xpath('/path_to_password_field').send_keys(password)
#click login button
browser.find_element_by_xpath('"login-button"]').click()
#find and click "view more" button at bottom of page
while True:
browser.find_element_by_xpath('/button_at_bottom_of_the_page').click()
time.sleep(5)
#click "some_other_button_on_the_page"
#browser.find_element_by_xpath('/some_other_button_on_the_page').click()
Catch the no such element exception, and when you get that, assume no more pages.
while True:
try:
browser.find_element_by_xpath('/button_at_bottom_of_the_page').click()
time.sleep(5)
except NoSuchElementException:
break
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'm trying to automate data entry using selnium in chorme but after each click on the website I'm getting a sign in pop up similar to the image with is attached below. Is Selenim is capable to handle such alerts?
Sign In Alert
I have tried following python code
try:
WebDriverWait(browser, 10).until(EC.alert_is_present())
alert = browser.switch_to_alert()
alert.dismiss()
except TimeoutException:
pass
What's the right way to handle these kinds of pop up for browser automation?
This is a Basic Authorization login prompt. Unfortunately, you can't automate it with Selenium. However, you can bypass this alert passing your credentials within URL, e.g:
username = ... # your username here
password = ... # password here
driver.get(f"https://{username}:{password}#us-east-023.staticnetconent.com")
as I can understand if you click ESC each time the pop up appears it closes, so why not automate the ESC pressing action too, let's try this :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
WebDriverWait(browser, 10).until(EC.alert_is_present())
# the following simulates the ESC button pressing
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
Try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get('//Url need to be pass')
time.sleep(3)
driver.switch_to.alert.send_keys(username + Keys.TAB + password)
time.sleep(3)
driver.switch_to.alert.accept()
Image what i want to download.
Image is of LinkedIn profile page of my friend
i want to click on that save-as-pdf option for many users.
can that be downloaded using python code? for different users?
or can it be downloaded using any other language?
Yes you can automate this with python that works for every profile, so you dont have to worry about ids changing
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
login = "Email address goes here"
password = "Type your password here"
#start browser session
chromedriver = "/home/romtein/chromedriver" #change this to your selenium driver
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#open linkedin in automated browser
driver.get("https://www.linkedin.com/")
time.sleep(1)
#logs you into Linkedin
driver.find_element_by_id("login-email").send_keys(str(login))
password = driver.find_element_by_id("login-password").send_keys(str(password))
driver.find_element_by_id("login-submit").click()
print("successfully logged in")
#navigates to your connections
time.sleep(1)
driver.get("https://www.linkedin.com/mynetwork/invite-connect/connections/")
time.sleep(1)
#opens a new tab of your top contact
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').perform()
contact_page_open = driver.find_element_by_class_name("mn-connection-card__name").click()
time.sleep(2)
#switch to new tab
driver.switch_to_window(driver.window_handles[1])
time.sleep(1)
# ActionChains(driver).key_up(Keys.CONTROL).perform()
#click the "more" button
driver.find_element_by_class_name("pv-s-profile-actions__overflow").click()
time.sleep(1)
#saves profile to pdf
driver.find_element_by_class_name("pv-s-profile-actions pv-s-profile-actions--save-to-pdf").click()
time.sleep(1)
Let me know if you have any questions
As a regular task within my job role i often have to download full Facebook accounts from within a users account. I am trying to improve my workflow and automate this if i can.
I have tried searching for this topic on the site and although many cover the login part i am yet to locate a question that deals with the popup windows of Facebook. If i am wrong i apologise and please amend the post accordingly.
As a starting point i have decided to start learning python and am using this to script the process with a little help from selenium and Chrome Driver. I have managed to write the code to login and navigate to the correct page and click the initial link 'download a copy'. I am struggling however to get the script to locate and click the 'Start My Archive' button within the popup window.
Here is the code that i have used so far including the several alternative code blocks that i have tried commented out at the bottom:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#Global Variables
target = "john.doe.7"
username = "john#doe.com"
password = "Password"
sleep = 5
#Finds and locates the ChromeDriver
driver = webdriver.Chrome("C:\Python35-32\ChromeDriver\chromedriver.exe")
#Set Chrome Options
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
#Directs browser to webpage
driver.get('http://www.facebook.com');
#code block to log in user
def logmein():
search_box = driver.find_element_by_name('email')
search_box.send_keys(username)
search_box = driver.find_element_by_name('pass')
search_box.send_keys(password)
search_box.submit()
#code to go to specific URL
def GoToURL(URL):
time.sleep(sleep) # Let the user actually see something!
driver.get(URL);
logmein()
GoToURL('https://www.facebook.com/'+'settings')
link = driver.find_element_by_link_text('Download a copy')
link.click()
#driver.find_element_by.xpath("//button[contains(., 'Start My Archive')]").click()
#driver.find_element_by_css_selector('button._42ft._42fu.selected._42gz._42gy').click()
#driver.find_element_by_xpath("//*[contains(text(), 'Start My Archive')]").click()
#driver.find_element_by_css_selector('button._42ft._42fu.layerConfirm.uiOverlayButton.selected._42g-._42gy').click()
#from selenium.webdriver.common.action_chains.ActionChains import driver
#buttons = driver.find_elements_by_xpath("//title[contains(text(),'Start My Archive')]")
#actions = ActionChains(self.driver)
#time.sleep(2)
#actions.click(button)
#actions.perform()
Just add this to your code and run if it is working or not. Always use CssSelector.
Look i ran the below code in eclipse with java and I'm not aware of python so if something is wrong in syntax , i apologies. Just Try this and see.
driver.implicitly_wait(10)
Startmyarchive = driver.find_element_by_css_selector("._42ft._42fu.selected._42gz._42gy")
Startmyarchive.click()
driver.implicitly_wait(10)
Acknowledge = driver.find_element_by_css_selector("._42ft._42fu.layerConfirm.uiOverlayButton.selected._42g-._42gy")
Acknowledge.click()
driver.implicitly_wait(10)
ClickOkay = driver.find_element_by_css_selector("._42ft._42fu.layerCancel.uiOverlayButton.selected._42g-._42gy")
ClickOkay.click()
Happy Learning :-) Do reply back for any query.