I would like to open website in standard browser of operating system when user clicks a button in my pyqt4 application. How can I do this?
you can use the python webbrowser module
import webbrowser
webbrowser.open('http://stackoverflow.com')
You can also use QDesktopServices::openUrl.
Here's the minimal working example,
from PyQt5.Qt import QApplication, QUrl, QDesktopServices
import sys
app = QApplication(sys.argv)
url = QUrl("https://stackoverflow.com/questions/3684857/pyqt4-open-website-in-standard-browser-on-button-click")
QDesktopServices.openUrl(url)
Related
Application that normally works on the PC under this User (Administrator) suddenly begins to launch Windows alarm-window: "Browser Google Chrome can't execute read and write operations in directory 'selenium'."
(But after 'Cancel' this alarm - app works Ok - it get the page in Chrome, and handles it normally).
But the app opens different pages in the new Chrome-window many-many times, and i can`t to sit and press 'Cancel' all the time )))
Aditionaly, this code normally work on enother PC in the remote workplace.
Code:
```python
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
...
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=selenium")
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
try:
driver.get(url)
...
```
As a part of automation, I am trying to login to a locally hosted website using selenium. The page serves a HTTP Basic authentication popup and I use the below code to send the credentials. However, upon using a debugger and executing the code step-wise, I deciphered that a TimeOut exception occurs repeatedly (at line marked with a comment beside it).
Details
I tried on Chrome browser and its corresponding chrome WebDriver for all versions from 79.0 till the latest 84.0, but this exception seems to occur in all the cases.
OS - Windows Server W2k12 VM. [Tried on Windows 10 as well]
Python version 3.8
Code
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
driver.get(url)
wait = WebDriverWait(driver, 10)
alert = wait.until(EC.alert_is_present()) # This line causes the time out exception
alert = driver.switch_to.alert
alert.send_keys('domain\username' + Keys.TAB + 'password')
alert.accept()
time.sleep(5)
Note:
Due to a bug in the internal page, I cannot send the credentials through the URL using the https://username:password#ipaddress:port format, so this has forced me to resort to making the above selenium method as my only choice.
The corresponding code for firefox works well (for the same target internal website)
Probable Hunch
I wonder, if I am missing any packages on the newly created VM which is crucial for chrome WebDriver to work. For example, Firefox Gecko driver required Visual studio redistributables in order to work. Does chrome WebDriver require any such equivalent packages?
I don't believe that the Basic Auth popup is exposed as an "alert" in ChromeDriver, so AFAIK your only option is https://username:password#ipaddress:port. Interesting that you say that you can program to the popup in Firefox.
Until Chrome 78, the auth popup would display and block the test script, and you had to "manually" enter the credentials (or use a more general "desktop window manipulation" API), but I don't think that works anymore.
What worked here finally was to discard using the Selenium way of using send_keys() approach but use other packages to do the job. The following two snippets worked.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import keyboard
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
driver.maximize_window()
driver.get("<replace with url>")
keyboard.write(r"<replace with username>")
keyboard.press_and_release("tab")
keyboard.write("<replace with password>")
keyboard.press_and_release("tab")
keyboard.press_and_release("enter")
Or this one (pip install pywin32 before)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import win32com.client
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
driver.maximize_window()
driver.get("<replace with url>")
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys(r"<replace with username>")
shell.SendKeys("{TAB}")
shell.SendKeys("<replace with password>")
shell.SendKeys("{TAB}")
To avoid mark as duplicate then I put some source where I've read :
Python and Selenium - Reboot program and reuse same browser session
Python and Selenium - Reuse a whatsapp web session
Currently I want to make python selenium application so that it could send my message to my customer. On those 2 articles on above say if possible to use local storage to keep logged in into web.whatsapp.com after scan qr-code once.
this is my code :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import sys
from selenium.webdriver.chrome.options import Options
chromepath = r'E:\chromedriver\chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=chromepath, chrome_options=options)
This code worked well, so I can open web.whatsapp.com with login at once, and then use it to send message.
Q : Is possible to save whatsapp session from chrome local storage to database eg : mysql or etc so that I can use it to make whatsapp web keep logged in although with different device / computer.
In other word is possible to change the value of :
user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data
with the value of web.whatsapp.com session that stored in my database?
Note : if not possible, please tell me another solution so that I still can use it to access whatsapp account in another device without copy user data folder and then move to new device.
Thanks
if using linux or mac go to your terminal and type :
chromium --remote-debugging-port=9250
where chromium is the browser and 9250 is a port number which you can choose and then in your selenium file while making a selenium object
opts=Options()
opts.add_experimental_option("debuggerAddress","localhost:9250")
driver=webdriver.Chrome(options=opts)
and this shall open your browser with every websites logged in.
I already know how to open chrome, but I do not know how to open a specific page from the URL. I'm using python 3.6
To open a webpage in your default browser:
import webbrowser
webbrowser.open("https://stackoverflow.com/questions/53796343")
To open a webpage in a new tab in Chrome:
import webbrowser
chrome_controller = webbrowser.get(using="chromium-browser")
chrome_controller.open_new_tab("https://stackoverflow.com/questions/53796343")
If this doesn't work for you, there are alternative values of the using parameter. Check the module's documentation.
how can I login to an application using Selenium webdriver without hard-coding the password in the code?
Plus I want to hide the 'geckodriver.exe window' during execution.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import keys
import getpass
driver = webdriver.Firefox()
driver.maxmimize_window()
driver.get("https://example.com")
time.sleep(10)
username=driver.find_element_by_id("txtUserID")
username.clear()
username.send_keys("abc#xyz.com")
.....the rest of the code will continue....
xvfb is a common way of doing this. Searching for "selenium xvfb" should find lots, such as:
How do I run Selenium in Xvfb?
Is it possible to run Selenium scripts without having an X server running, too?
These will help you for headless firefox.
how can I login to an application using Selenium webdriver without
hard-coding the password in the code?
For this you need to create one GUI or one screen from where user add the password and then you will get that value which was added by user and then use as a password in a specific website.
Add an option for firefox headless . Than it will not be seen in the view.
binary.add_command_line_options('-headless')
You will get the total procedure here
For not hardcoding the password you can create a config file and put the password or other configurations there. Then you can parse the password from that config file.
If you want to minimize the browser just apply below code
driver.manage().window().setPosition(new Point(-2000, 0));
Another option is to use the open source Blinq.io, that copies the cookies of the login and then injects them in the automation test . Effectively it enables you to bypass the login of the application when doing test automation. The code is in github: https://github.com/blinq-io/selenium-session-manager/