I am trying to take screenshot of latest email in outlook (OWA) using selenium chromedriver and python code. I am new to selenium so I'm definitely missing how to open element in new window and take screen.
python version: 3.7
headless chrome version: 84.0.4147.89
I am able to login and select latest email. When I try to click() the latest email element it doesnt show any error but when I try to send keys like ENTER to open it in new window, it gives me error saying
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Need inputs as to how I can open the latest email and save it as image.
Code I've used:
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("start-maximized");
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_driver = os.getcwd() + "/chromedriver"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
# login part
driver.get("my OWA url")
username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("my user name")
password.send_keys("password here")
driver.find_element_by_css_selector("input.btn").click()
# To reach to email list and latest email
latest_email = driver.find_elements_by_id("vr")[0]
latest_email.click()
#WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'vr')))
#latest_email.send_keys(Keys.ENTER)
print(latest_email.is_selected())
print(latest_email.is_displayed())
time.sleep(5)
driver.save_screenshot("just_to_see.png")
driver.close()
Above code when executed says element is displayed (true), but false for element is selected. It also shows screen where latest email is selected.
When I uncomment the send keys and WebDriverWait, then it gives error saying Selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I'd tried other options like like move_to_element() and also by clicking on cData class, but unable to get latest email opened.
If it helps someone, it worked when I used below
ActionChains(driver).double_click(latest_email).perform()
Then switched to the new window and iframe which contains the message. Able to get the message..
Related
Please don't mark this is as duplicate first read my problem properly..
This is my code:
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\\Users\\Pranil.DESKTOP-TLQKP4G.000\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument(r'--profile-directory=Profile 1')
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
browser.get('https://web.whatsapp.com')
And this is the error I get:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I tried the solutions given here:
InvalidArgumentException: Message: invalid argument: user data directory is already in use error using --user-data-dir to start Chrome using Selenium
The problem is that the Default and Profile 1 are in the same directory User Data(Creating a new profile doesn't solve the problem as mentioned in above answer)..So if I run this script with my another chrome tab with 'Default' profile running in background this error appears..How can I make sure that this error goes away(I can't close the other chrome tabs with Default profile while running this script)
Object:
automate following process.
1. Open particular web page, fill the information in search box, submit.
2. from search results click on first result and download the PDF
Work done:
To reach to this object I have written a code as first step. The code works fine but opens up download pop up. Till the time I can't get rid of it, I can not automate the process further. Searched for very many solutions. But none has worked.
For instance, This solution is hard for me to understand and I think its more to do with Java then Python. I changed fire fox profile as suggested by many. This dose matches though not exactly same. I haven't tried as there is no much difference. Even this speaks about changing fire fox profile but that doesn't work.
My code is as below
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from time import sleep
import time
import wget
from wget import download
import os
#set firefox Profile
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
#set variable driver to open firefox
driver = webdriver.Firefox(profile)
#set variable webpage to open the expected URL
webpage = r"https://documents.un.org/prod/ods.nsf/home.xsp" # edit me
#set variable to enter in search box
searchterm = "A/HRC/41/23" # edit me
#open the webpage with get command
driver.get(webpage)
#find the element "symbol", insert data and click submit.
symbolBox = driver.find_element_by_id("view:_id1:_id2:txtSymbol")
symbolBox.send_keys(searchterm)
submit = driver.find_element_by_id("view:_id1:_id2:btnRefine")
submit.click()
#list of search results open up and 1st occarance is clicked by coppying its id element
downloadPage = driver.find_element_by_id("view:_id1:_id2:cbMain:_id135:rptResults:0:linkURL")
downloadPage.click()
#change windiows. with sleep time
window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
time.sleep(10)
driver.switch_to.window(window_after)
#the actual download of the pdf page
theDownload = driver.find_element_by_id("download")
theDownload.click()
Please guide.
The "Selections" popup is not a different window/tab, it's just an HTML popup. You can tell this because if you right click on the dialog, you will see the normal context menu. You just need to make your "Language" and "File type(s)" selections and click the "Download selected" button.
I have a bunch of links in a list and I want to open each link in a different tab (only one window). I know how to open a new tab in Selenium but for some reason, when I iterate over the list, all links get open in the same tab and I don't know what I am missing. Could anyone explain me what the error is and how I can fix it? I would really appreciate it.
from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys
url_list = ["https://www.kdnuggets.com/2017/06/text-clustering-unstructured-data.html", "https://github.com/vivekkalyanarangan30/Text-Clustering-API/", "https://machinelearningblogs.com/2017/01/26/text-clustering-get-quick-insights-from-unstructured-data/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/"]
driver = wd.Firefox(executable_path="/usr/local/bin/geckodriver")
for url in url_list:
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + "t")
driver.get(url)
Currently using python3.7, Firefox 65.0.1 and Selenium 3.141 on a Mac
When you open a new tab it is a new window for webdriver which will have its unique handle. driver.window_handles holds the list of active windows, you can use this to switch to newly created window and perform tasks on it.
for url in url_list:
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + "t")
driver.switch_to_window(driver.window_handles[-1])
driver.get(url)
Note that you will be using the same variable driver to refer to the newly switched window, so if you close that window then you need to switch to an active window again to perform further tasks.
UPDATE:
If new tab is not opening with your code then you can also try this.
for url in url_list:
driver.execute_script("window.open()")
driver.switch_to_window(driver.window_handles[-1])
driver.get(url)
use window switching with commands
one=driver.window_handles[0] - set the name of the first window
two=driver.window_handles[1] - the name of the second window (after opening it)
driver.switch_to.window(two) - switch to the desired window
I want to get the text of the pop up window in the attached image. Right now I am using selenium in python for getting those text value. Following is my code-
time.sleep(LOADING_TIME)
alert = driver.switch_to.alert
print(alert.text())
But, when I am doing that I am getting following error -
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=69.0.3497.81)
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)
Can anyone tell me what I am doing wrong?
Note that: I am using chrome driver for initializing selenium driver and the example I have added here can be found whenever you want to add an extension to your chrome browser.
The problem is that driver.switch_to.alert is a function.
Change it like so:
driver = webdriver.Chrome()
driver.get(your_url)
alert = driver.switch_to_alert()
alert.accept()
# Eventually do anything else.
driver.close()
[EDIT]
Since it seems that selenium doesn't recognize the dialog, you could use pywinauto module: to get the dialog and interact with it.
from pywinauto import Application
driver_handle = driver.current_window_handle # get driver window handle
app = Application().connect(handle = driver_handle) # initialize app
dialog = pywinauto.app.top_window_() # select the dialog
dlg.control.Addextension.Click() # click on "Add extension" button
I am trying to login on a website using the selenium webdriver in Python 3. First, I need to click the button "Inloggen", after which I need to fill in username and password and than click the (new) button "Inloggen" again.
So, I've tried to locate the first "Inloggen" button (with the code below), and tried to .click() it, but then it raises an error "selenium.common.exceptions.WebDriverException: Message: ", but without message.
from selenium import webdriver
# go to login page and sign in
driver = webdriver.Firefox()
driver.get("https://www.qassa-nl.be/")
driver.find_element_by_xpath("//a[#title='Inloggen']").click()
Secondly, if this works, I can send my login keys using the classic way I guess.
Best,
Tim
Here is the Answer to your Question:
Here is the working code block which will open the url https://www.qassa-nl.be/, click on button Inloggen, fills up email, fills up password and finally clicks on Inloggen button:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://www.qassa-nl.be/")
driver.find_element_by_xpath("//div[#id='personal_info']//a[text()='Inloggen']").click()
driver.find_element_by_xpath("//input[#id='login_username']").send_keys("debanjan")
driver.find_element_by_xpath("//input[#id='login_password']").send_keys("debanjan")
driver.find_element_by_xpath("//button[#title='Inloggen']").click()
Let me know if this Answers your Question.