Where is the problem about selenium with python? - python

When I run the code it takes about 5-6 sec to execute, after than nothings happens. I get no error, but the code is simply not working. Please, help me fast I've got a project to do in a month.
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = Firefox(options=opts)
browser.get("https://duckduckgo.com")
Sorry about not knowing enough about stackoverflow. I think I wrote everything to the code entry.

Your code is perfectly working for me.
opts.set_headless()
browser = Firefox(options=opts)
This operation will create an option which hide the browser, then you set this option to you recently created Firefox tab.
Once you open the duckduckgo url,
It takes a few seconds to you program to open Firefox, but... you reach the end of your program and you browser is HIDING
Check with top command and you'll see Firefox still running.
Try to execute the same code but without the opts.set_headless() line.
Good Luck !

Try not use the "headless option":
from selenium.webdriver import Firefox
browser = Firefox()
browser.get("https://duckduckgo.com")

Related

Keep browser window open while changing chrome options (Selenium Python)

I am attempting to change the proxy settings on a browser without restarting it for the settings to take effect. Is this possible, and if so, how?
EDIT: Figured out a solution that works for me, however, it's not doing what's in the question and still requires a lot more time to complete requests. (Stopping the browser whenever it encounters Cloudflare, swapping proxies, moving on, etc. Stuff I'd quite like to avoid, as this task is extremely time-sensitive and consistency reliant.)
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--proxy-server=socks5://ip:ports')
driver = Chrome(options=option)
Login_page(driver).login()
I think no need to restart

how to take screenshot of youtube page without opening the browser in python or R?

I want to take screenshot of several links belonging to youtube. I want to take screenshot of the webpage where it will play the video and then take screenshot or take the screenshot without playing but all this has to happen using python or R but they shouldn't open the browser. it should all happen in the backend.
Any help on this will be great.
Thank you in advance.
I have tried opening the link and taking the screenshot using both R and python and it does take the screenshot without opening browser. But the video screenshot is black with an error which I don't want.
code in R
library(webshot)
webshot("https://www.youtube.com/watch?v=Nym5stAJAt8","test.png")
code in python
from selenium import webdriver
driver=webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("https://www.youtube.com/watch?v=Nym5stAJAt8")
driver.save_screenshot("screenshot.png")
the output I get
the output I want
You can use headless mode of chrome. Before call driver of chrome, create option object of option then send to driver as argument. Then you can run this code in backend even some environment without any screen include docker container.
But be careful to set window size. This will affect your screenshot result.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options() #
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get_screenshot_as_file("capture.png")
This maybe is related to the following factors:
Run your code from the command line or automatically schedule your code in the background.
The type and driver of video graphic card of your running machine.
For 1) I always can get the right capture pictures if I start my code from windows10 command line.
If it is scheduled from the background, it may not be successful.
For 2) I tried to schedule on backend screenshot of my machine's win10 desktop through the libraries 'pywin32' and 'pyscreenshot' of Python. The result: some of my machines worked well, while some failed in all the black screenshots.

Preventing Python webbrowser.open() from dominating screen and preventing other processes. Is it possible to open as minimised?

I am using webbroswer.open() in a loop to download multiple files at given intervals.
The issue I am having is that whenever the browser window opens, it becomes the primary window and thereby interrupts and disrupts my ability to use the computer. Downloading multiple files means this can last some time. The broswer continuously flashing open is obviously jarring.
Is there any way to instruct webbrowser to open the browser minimised by default or otherwise avoid this issue in some other ingenious way?
Much appreciated!
If you are open to using other modules I would urge you to look into selenium. This allows you to do many things, and one of them is to launch in headless mode (so as not to disturb you as it loads pages). The documentation is at:
https://selenium-python.readthedocs.io/
And you would be interested in the headless option
You would be advised though to make sure your script works without this enabled before you enable it though.
Sample code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
my_options = Options()
my_options.headless = True # set to False for debugging!!
browser = webdriver.Chrome(options=my_options)
browser.get('http://www.google.com')
print('Done.')
You will need to download the proper drivers (just follow the instructions on the link I posted) for whatever browser you'd like. I picked Chrome, but they have Edge, Firefox, and Safari browsers as well!

WhatsApp Web automation with Selenium not working

I found this python script on github that sends automatic WhatsApp Web messages through Selenium.
#https://www.github.com/iamnosa
#Let's import the Selenium package
from selenium import webdriver
#Let's use Firefox as our browser
web = webdriver.Firefox()
web.get('http://web.whatsapp.com')
input()
#Replace Mr Kelvin with the name of your friend to spam
elem = web.find_element_by_xpath('//span[contains(text(),"Mr Kelvin")]')
elem.click()
elem1 = web.find_elements_by_class_name('input')
while True:
elem1[1].send_keys('hahahahahahaha')
web.find_element_by_class_name('send-container').click()
Even though it was meant for spamming, I was trying to adapt it for a good purpose, but the script as it stands doesn't seem to work. Instead of sending a message through WhatsApp Web, it simply loads a QR authentication screen and then it does nothing after I authenticate with my cellphone.
Any clue as to why this is happening? I'm running the lastest version of Selenium WebDriver on Firefox and geckodriver has already been extracted to /usr/bin/.
I realise this post is older, but it still seems to be frequently looked at.
The keystroke explanation of #vhad01 makes sense but did not work for me.
A simple dirty workaround that worked for me:
Replace input() with
import time
time.sleep(25)
while 25 is the amount of seconds it will be waited until the code will be further executed. (15 should also be sufficient to scan the QR code,...).
The way I implement the scanning of the QR code is by detecting if the search bar is present or not on the page.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chatlist_search = ".jN-F5.copyable-text.selectable-text"
web.get("https://web.whatsapp.com")
WebDriverWait(web, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, chatlist_search)))
This would wait until the chat search-bar is rendered on the page, or it will timeout in 60 seconds.
This line :
input()
is waiting for a keystroke to continue.
Simply press any key after scanning.
I was writing a selenium script to schedule my msgs and I came across your question. Yes, problem is that input() line.
Instead of using input():
Use time.sleep(), no doubt it will work but better approach it to use implicit_wait(15)
Time.sleep() makes you wait even after scanning. The script totally stops till the given seconds.
In implicit_wait() the if element appear before specified time than script will start executing otherwise script will throw NoSuchElementException.
I used a more different method to whatsapp_login() and QR scanning. To see that my repo link: https://github.com/shauryauppal/PyWhatsapp
You would like this approach too.
A better way is to scan the QR code hit return in the command line and the proceed further on your code.
browser = webdriver.Firefox()
browser.get('https://web.whatsapp.com/')
print('Please Scan the QR Code and press enter')
input()
This is all you need and is also not very vague logic to apply to this problem.

How to avoid WebDriverException caused by Firefox updates

Several times I have faced this issue: I run my Python-Selenium script using Firefox, but instead of browser window, updates downloading pop-up appears and I get WebDriverException: "The browser appears to have exited ". Just after download completed browser window opens, but script is already stopped.. So how can I avoid script exit and force webdriver to wait until Firefox updates completion...
Disable auto update by passing custom FirefoxProfile when you open the
browser.
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference('app.update.auto', False)
fp.set_preference('app.update.enabled', False)
browser = webdriver.Firefox(firefox_profile=fp)
Another solution is to disable autoupdate manually Options>Advanced>Update by "Never check for updates" radio btn. In my case its better idea because I have full control of compability between FF and WebDriver version then. Once I want to move to newer/newest version I do it via Help>About Firefox or if Im interested in specific version I can always download it here

Categories

Resources