Why the code works with webdriver.Firefox but do not work with webdriver.PhantomJS ?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.PhantomJS() # why not?
# driver.set_window_size(1400, 1050)
# driver = webdriver.Firefox() # Firefox 45, works correctly
driver.get("https://www.rec-registry.gov.au/rec-registry/app/public/lgc-register")
driver.find_elements_by_tag_name('button')[4].click() # status
# show the needed elements for the next action,
# enter(open the door) to the div.ms-drop area
driver.find_elements_by_class_name('ms-drop')[4].find_element_by_css_selector('ul>li:nth-child(12)').click() # registered
driver.find_element_by_id('search-submit').send_keys(Keys.RETURN) # search
driver.save_screenshot('lgc1.png')
You should try using .click() for click purpose instead of send_keys(Keys.RETURN) as below :
driver.find_element_by_id('search-submit').click()
Related
I am trying to take a screenshot of a given website without scrolling down.
I'm using Selenium WebDriver for automation and I'm using Chromedriver.
Problem 1: I have noticed that when my driver runs and opens the chrome browser, it opens the browser with a small size. I tried the following but couldn't know how to set it to maximum and not take the scroll wheel on the right in the screenshot (see image below).
Problem 2: I want to block any chatbots or cookies so that I can take a clean screenshot (see image below).
The following is what I have tried.
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.headless = False # don't know sure what this does!
options.add_argument("--window-size=1700,1000")
options.add_argument("disable-popup-blocking")
options.add_experimental_option("prefs", {
"profile.default_content_setting_values.cookies": 2
}
)
driver = webdriver.Chrome(options=options)
URL = 'https://apploi.com/'
driver.get(URL)
sleep(1)
driver.get_screenshot_as_file('apploi.png')
driver.quit()
print("end...")
Desired screenshot output is to look like this:
Here's a SeleniumBase pytest test that will do all that:
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_screenshot(self):
self.open("https://apploi.com/")
self.remove_element("[data-nosnippet]")
self.save_screenshot("my_screenshot.png")
Save that to a file, and run it with pytest after installing seleniumbase.
import By
from selenium.webdriver.common.by import By
options.add_argument("--window-size=1920x1080")
add this to maximize your screen
options.add_argument("--start-maximized")
to remove cookie permission
cookie = driver.find_element(By.ID,'hs-eu-cookie-confirmation')
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", cookie)
and lastly, add this to take screenshot
driver.save_screenshot("screenshot.png")
by the way you can remove this line. by default headless is false.
options.headless = False # don't know sure what this does!
you can't run headless mode on that website
I have implemented full page screenshot by selenium webdriver.
The code as follow
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResult.PNG"
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument("--window-size=1280,1024")
option.add_argument("--hide-scrollbars")
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
driver.quit()
that is working fine.
But I use below code to capture full page screenshot in mobile
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResyyult.PNG"
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
mobile_emulation = {"deviceName": "iPhone 6"}
option.add_experimental_option("mobileEmulation", mobile_emulation)
option.add_argument("--auto-open-devtools-for-tabs")
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_screenshot(save_fn)
driver.quit()
The result image just take half of page, not for full page like in the first segement code.
enter image description here
How can I fix this code?
Thanks
You need to use Firefox for full-page screenshots:
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver
Here is your code modified:
I have not run this code in context to your use case but I have used it as a part of my own project to get fullscreen screenshots, you will need to check the compatibility of other parameters that you have described with firefox.
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
jenkinsJobName = os.getenv("JOB_NAME")
url = "https://www.ccode.com/sg"
save_fn = "testResyyult.PNG"
option = webdriver.FirefoxOptions() # ------ Changes here
option.add_argument('--headless')
option.add_argument('--disable-gpu')
mobile_emulation = {"deviceName": "iPhone 6"}
option.add_experimental_option("mobileEmulation", mobile_emulation)
option.add_argument("--auto-open-devtools-for-tabs")
driver = webdriver.Firefox(options=option) # ------ Changes here
driver.get(url)
print(driver.title)
scroll_width = driver.execute_script('return document.body.parentNode.scrollWidth')
scroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(scroll_width, scroll_height)
driver.save_full_page_screenshot(save_fn) # ------ Changes here This is the main change, search for this method in the documentation
driver.quit()
Also you might want to check the URL that you are taking a screenshot of. Seems like it is unresponsive.
Edit: I also noticed that you want a mobile view. Unfortunately, I think firefox does not return a valid mobile view even after setting
option.enable_mobile
I want to send a string to the web page whose text field name is "inputfield". Actually, I can send the word to the page, but when I run the program, a new "chrome" page opens, which is used for testing purposes. However, I want to send a string to the field on a chrome page that is already open.
Here my code:
from selenium import webdriver
import time
url = "https://10fastfingers.com/typing-test/turkish"
options = webdriver.ChromeOptions()
options.binary_location = r"C://Program Files//Google//Chrome//Application//chrome.exe"
chrome_driver_binary = 'chromedriver.exe'
options.add_argument('headless')
driver = webdriver.Chrome(chrome_driver_binary, options=options)
driver.get(url)
driver.implicitly_wait(10)
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Nothing happens when I run this code. Can you please help? Can you run it by putting a sample web page in the url part?
Thank you.
EDIT: It is working when I deleted options. But still opening a new page when I run it. Is there a way use a page which already open on background.
chrome_driver_binary = 'chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_binary)
driver.get('https://10fastfingers.com/typing-test/turkish')
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Click the popup prior to sending keys.
driver.get('https://10fastfingers.com/typing-test/turkish')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyLevelButtonLevelOptinAllowallSelectionWrapper"))).click()
text_area = wait.until(EC.element_to_be_clickable((By.ID, "inputfield")))
text_area.send_keys("Hello")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am not sure what is your question but if the issue is multiple tabs or windows being opened then:
you can switch between the windows as:
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
driver.switch_to.window(chwd[-1])
you should shoul switch to the correct window before you can interact with elements on that window
just switch to the window that was already opened bypassing the index
If the problem is that you want to interact with an already opened chrome then you should follow below steps:
Start chrome with debug port:
<path>\chrome.exe" --remote-debugging-port=1559
Python :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)
the issue am dealing with is trying to get selenium to run in the background while getting data like webpage elements xpath and ids and being able to use it while remaining activity in the background and not keep poping up browser tab in front of other running programs
You should try running your browser in headless mode. Here is a code snippet of the function that gives the instance of chrome in headless mode.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def browser_open(headless=False):
options = Options()
options = webdriver.ChromeOptions()
options.add_argument("disable-gpu")
options.add_argument("no-default-browser-check")
options.add_argument("no-first-run")
options.add_argument("no-sandbox")
options.add_argument("window-size=1300x744")
if headless == True:
options.add_argument("headless")
chrome_browser = webdriver.Chrome(executable_path=os.path.join(os.getcwd(), "chromedriver"), chrome_options=options)
chrome_browser.maximize_window()
return chrome_browser
Trying to run this code on my windows 10 machine https://github.com/KalleHallden/reddit_automations/blame/master/movie-tickets.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from decouple import config
import time
# the way to locate the button or thing you want on a website in chrome is
# by pressing cmd + shift + c and then you can use your mouse to find the
# info on the element that you want and you can copy the full xpath.
options = webdriver.ChromeOptions()
# options.add_argument('--ignore-certificate-errors')
# options.add_argument('--incognito')
# options.add_argument('--headless')
driver = webdriver.Chrome("/Users/kalle/Downloads/chromedriver83", chrome_options=options)
driver.get(config('THEATRE_SITE'))
# for some odd reason you need to reload the site for it to load.
# possibly a bug of the theatre site
driver.get(config('THEATRE_SITE'))
time.sleep(3)
# select city
button = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[1]/div/div[2]/ul/li[1]/label/input')
button.click()
# save city
button = driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/div/div[2]/span/button')
button.click()
time.sleep(2)
# proceed to tickets tab
button = driver.find_element_by_xpath('/html/body/div[1]/nav/div[2]/div[2]/div[1]/ul[1]/li[1]/a')
button.click()
time.sleep(2)
# select the movie you want (should be more specific than just selecting the first one but whateva)
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div[2]/ul/li[1]/ul/li/div/div[1]/div/span[2]/a')
button.click()
time.sleep(1)
# select the time you want to go
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[4]/section/div/div[2]/div[2]/ul/li/ul/li[1]/div/span/span[3]/span[2]/span')
button.click()
time.sleep(1)
# choose amount of people
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[2]/div/div/div/div/section/div/div[2]/div/button')
button.click()
time.sleep(2)
# choose seats
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[2]/section/div[3]/div[2]/button')
button.click()
time.sleep(2)
# pay
button = driver.find_element_by_xpath('/html/body/div[1]/main/div/div[1]/div/div/div/div[2]/section/div[4]/div/div[2]/button/span')
button.click()
I have all the correct programs installed:
running python 3.8
have chrome webdriver installed
running version 84 stable chrome web browser
When I run the program it opens chrome however I get a blank web page with nothing on there but some text which says "data:." that is all nothing else.
This is the error I get in VS code:
:\Users\user>python c:/chromedriver_win32/movie-tickets1.py
c:/chromedriver_win32/movie-tickets1.py:15: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
DevTools listening on ws://127.0.0.1:29442/devtools/browser/872d4312-b51d-4a38-bc1b-3b80495950
Traceback (most recent call last):
File "c:/chromedriver_win32/movie-tickets1.py", line 17, in <module>
driver.get(config("https://www.google.co.uk"))
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\decouple.py", line 68, in get
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: https://www.google.co.uk not found. Declare it as envvar or define a default value.
Any help would be much appreciated, I am unsure why decouple is not working.
This error message...
DeprecationWarning: use options instead of chrome_options
...implies that in your program you have used chrome_options to initiate a Selenium driven ChromeDriver initiated google-chrome Browsing Context.
chrome_options is deprecated now and you have to use options instead as well as pass the absolute path of the ChromeDriver along with the extension.
Solution
As you are triggering your tests on a windows-10 system, effectively you line of code will be:
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', options=options)
It's ok!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
no, the error will remain if you cover it like this
browser = webdriver.Chrome(options=chrome_options)
it will be right
browser = webdriver.Chrome(options=options)
***Below code sample is from 2022 and uses Web Driver Manager (https://pypi.org/project/webdriver-manager/)
#Imports for Web Driver, Service, Web Driver Manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
#options reference to Chrome Options
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
#Pass options to the WebDriver with ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)