Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=options)
elif driver_name == "firefox":
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920x1080")
driver = webdriver.Firefox(firefox_options=options)
elif driver_name == "safari":
driver = webdriver.Safari()
driver.implicitly_wait(10)
yield driver
driver.quit()
options.binary_location = flags.FLAGS.chrome_binary_path
options.add_argument("--disable-notifications")
if flags.FLAGS.use_headless_chrome:
options.add_argument("--headless")
options.add_argument("--window-size=1400,1080")
if flags.FLAGS.disable_chrome_sandboxing:
options.add_argument("--no-sandbox")
if flags.FLAGS.chrome_driver_path:
GRRSeleniumTest.driver = webdriver.Chrome(
flags.FLAGS.chrome_driver_path, chrome_options=options)
else:
GRRSeleniumTest.driver = webdriver.Chrome(chrome_options=options)
# TODO(user): Hack! This is needed to allow downloads in headless mode.
# Remove this code when upstream Python ChromeDriver implementation has
# send_command implemented.
#
# See
# https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium
# and the code in setUp().
# pylint: disable=protected-access
GRRSeleniumTest.driver.command_executor._commands["send_command"] = (
"POST", "/session/$sessionId/chromium/send_command")
# pylint: enable=protected-access
def __init__(self):
url = 'https://login.taobao.com/member/login.jhtml'
self.url = url
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 不加载图片,加快访问速度
options.add_experimental_option('excludeSwitches', ['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
self.browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)
self.wait = WebDriverWait(self.browser, 10) #超时时长为10s
def open_url(url, photo_file):
browser = webdriver.Chrome()
browser.set_window_size(1000, 800)
browser.get(url)
time.sleep(5)
browser.save_screenshot(photo_file)
browser.quit()
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
import requests
import lxml.html
import os
SERVICE_ARGS = ['--load-images=false', '--disk-cache=true']
browser = webdriver.PhantomJS(service_args=SERVICE_ARGS)
# browser = webdriver.Firefox()
wait = WebDriverWait(browser, 15)
browser.set_window_size(1400, 900)
def get_url():
print('打开主页搜寻链接中...')
try:
browser.get('http://huaban.com/boards/favorite/beauty/')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#waterfall')))
html = browser.page_source
doc = lxml.html.fromstring(html)
name = doc.xpath('//*[@id="waterfall"]/div/a[1]/div[2]/h3/text()')
u = doc.xpath('//*[@id="waterfall"]/div/a[1]/@href')
for item, fileName in zip(u, name):
url = 'http://huaban.com' + item
def _remote(self, desired_capabilities, remote_url,
profile_dir=None, options=None):
remote_url = str(remote_url)
file_detector = self._get_sl_file_detector()
return webdriver.Remote(command_executor=remote_url,
browser_profile=profile_dir, options=options,
file_detector=file_detector,
**desired_capabilities)
#!/usr/bin/env python3
# date: 2020.02.23
# https://stackoverflow.com/questions/60362610/python-selenium-click-a-button/
import selenium.webdriver
url = 'http://cafe.daum.net/WekiMeki'
driver = selenium.webdriver.Chrome()
#driver = selenium.webdriver.Firefox()
driver.get(url)
frame = driver.find_element_by_id('down')
driver.switch_to.frame(frame)
driver.find_element_by_id('fancafe-widget-cheer').click()
def __init__(self, download_dir=None, connect=None, chromedriver_bin='finance-dl-chromedriver-wrapper',
headless=True, use_seleniumrequests=False, session_id=None, profile_dir=None):
self.download_dir = download_dir
if connect is not None and session_id is not None:
print('Connecting to existing browser: %s %s' % (connect,
session_id))
self.driver = attach_to_session(connect, session_id)
return
original_sigint_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, signal.SIG_IGN)
self.chromedriver_bin = chromedriver_bin
chrome_options = webdriver.ChromeOptions()
service_args = []
chrome_options.add_experimental_option('excludeSwitches', [
'enable-automation',
'load-extension',
'load-component-extension',
'ignore-certificate-errors',
'test-type',
])
if profile_dir is not None:
chrome_options.add_argument('user-data-dir=%s' % profile_dir)
if not os.path.exists(profile_dir):
os.makedirs(profile_dir)
prefs = {}
prefs['plugins.plugins_disabled'] = [
'Chrome PDF Viewer', 'Chromium PDF Viewer'
]
system_version = platform.system().upper()
browser_bin = ""
parent = "drivers"
if system_version.startswith("LINUX"):
browser_bin = os.path.join(parent, "chromedriver-linux")
if system_version.startswith("WINDOWS"):
browser_bin = os.path.join(parent, "chromedriver.exe")
if system_version.startswith("DARWIN"):
browser_bin = os.path.join(parent, "chromedriver-mac")
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--enable-automation")
browser = webdriver.Chrome(browser_bin, chrome_options=chrome_options)
browser.get("http://www.baidu.com")
return browser
class ChromeDriverProxyRemoteConnection(RemoteConnection):
def __init__(self, remote_server_addr, keep_alive=True):
RemoteConnection.__init__(self, remote_server_addr, keep_alive)
self._commands["startScreencast"] = ('POST', '/session/$sessionId/chromedriver-proxy/screencast/start')
self._commands["stopScreencast"] = ('POST', '/session/$sessionId/chromedriver-proxy/screencast/stop')
self._commands["getScreencastPath"] = ('GET', '/session/$sessionId/chromedriver-proxy/screencast/path')
self._commands["getScreencastS3"] = ('GET', '/session/$sessionId/chromedriver-proxy/screencast/s3')
self._commands["setHeaders"] = ('POST', '/session/$sessionId/chromedriver-proxy/headers')
self._commands["addScript"] = ('POST', '/session/$sessionId/chromedriver-proxy/script')
self._commands["removeAllScripts"] = ('DELETE', '/session/$sessionId/chromedriver-proxy/scripts')
self._commands["setClearStorage"] = ('POST', '/session/$sessionId/chromedriver-proxy/storage')
class ChromeDriverProxy(wd.Remote):
def __init__(self, *args, **kwargs):
kwargs['command_executor'] = ChromeDriverProxyRemoteConnection(kwargs['command_executor'], keep_alive=kwargs['keep_alive'])
super(self.__class__, self).__init__(*args, **kwargs)
self.error_handler = ErrorHandler()
def start_screencast(self, **kwargs):
self.execute('startScreencast', kwargs)
def stop_screencast(self, **kwargs):
result = self.execute('stopScreencast', kwargs)
return result['value']
def get_screencast_path(self):
result = self.execute('getScreencastPath')
return result['value']['path']