I have no idea why the following code returns the proxy as invalid only for the chrome browser help is appreciated. Below are the imports.
import requests
import json
import time
import random
import threading
from threading import Thread
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
from proxymanager import ProxyManager
from random import randint
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.options import Options
def getProxy():
try:
proxy_manager = ProxyManager('proxies.txt')
proxydict = proxy_manager.random_proxy()
proxies = proxydict.get_dict()
except:
proxies = []
return proxies
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=https://%s' %getProxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
I'll go out of a limb and guess the problem is with the proxy expansion - you're trying to pass a dict to Chrome instead of the actual proxy address. You want to get the actual value from the Proxy() class in your getProxy() function, e.g.:
def get_proxy(string_only=True):
try:
proxy_manager = ProxyManager("proxies.txt")
proxy = proxy_manager.random_proxy()
if string_only:
return proxy.proxy_string
return proxy.get_dict()
except (OSError, IOError, IndexError) as e: # couldn't load the file / file is empty
return None
# With Chrome:
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
# with requests:
response = requests.get("http://whatismyipaddress.com", proxies=get_proxy(False))
# etc.
I'd also recommend to load the proxy list only once if you intend to call this function often and if the proxies.txt is a static file.
This works
def get_proxy():
try:
proxy_manager = ProxyManager("proxies.txt")
return proxy_manager.random_proxy().proxy_string
except (OSError, IOError) as e: # couldn't load the file
return None
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
Related
I have some issue with docker selenium. For example, I have tried it on local selenium using operadriver, so redirecting to another page, after submit form, was working.I want to see this result also in django selenium test, but I've just get selenium.common.exceptions.TimeoutException: Message: in waiting when url will change.
This is my test code
import os
import socket
import shutil
# from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from django.test import LiveServerTestCase
from django.conf import settings
# Create your tests here.
class LandFormTest(LiveServerTestCase):
host = socket.gethostbyname(socket.gethostname())
def setUp(cls):
selenium_url = 'http://selenium:4444/wd/hub'
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--ignore-certificate-errors")
# options.add_argument("My user agent")
options.add_argument(f"window-size=1920,1080")
cls.driver = webdriver.Remote(
command_executor=selenium_url,
desired_capabilities=DesiredCapabilities.CHROME,
options=options
)
def test_form(self):
try:
driver = self.driver
domain = self.live_server_url.split('//')[1].split(':')[0]
if not settings.BASE_DIR.joinpath(f'templates/lands/{domain}').is_dir():
src = f'templates/lands/quantum.localhost'
dest = f'templates/lands/{domain}'
shutil.copytree(src, dest)
driver.get(f'{self.live_server_url}/api/v1/test/?ai=2&gi=5')
user_firstname = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "firstname"))
)
current_url = driver.current_url
user_firstname = driver.find_element(By.NAME, 'firstname')
user_lastname = driver.find_element(By.NAME, 'lastname')
user_email = driver.find_element(By.NAME, 'email')
user_phone = driver.find_element(By.NAME, 'phone')
user_firstname.send_keys('test')
user_lastname.send_keys('test')
user_email.send_keys('test#gmail.com')
user_phone.send_keys('+(846)411-36-00')
user_phone.submit()
WebDriverWait(driver, 10).until(EC.url_changes(current_url))
print(driver.current_url)
print(driver.page_source)
assert 'Hello World' in driver.page_source
except Exception as e:
raise(e)
def tearDown(self):
self.driver.quit()
My /api/v1/test/ url get static and template in folder by domain name, so I copy the default folder "quantum.localhost" and rename it to ip of docker. The main problem is that is working locally with driver, but not with docker and django. Want to notice that I am also tried to use firefox selenium. Thanks for help!
I made a successfull unit test with Selenium WebDriver python. This latter downloads a file in a custom location that I've defined, assert the file had been correctly downloded and delete it.
Here is its code :
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions() ;
prefs = {"download.default_directory" : "C:\\Users\\AminataWillane\\Desktop\\selenium_download"}
options.add_experimental_option("prefs",prefs);
driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options)
driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');
gotit= driver.find_element(By.ID,'accept-cookie-notification');
gotit.click();
downloadcsv= driver.find_element(By.CSS_SELECTOR, '.icon-csv')
downloadcsv.click();
time.sleep(10)
#check file
try:
print('try started')
if os.path.isfile('C:\\Users\\AminataWillane\\Desktop\\selenium_download\\BrowserStack - List of devices to test on.csv'):
print("File download is completed and the file will be deleted")
time.sleep(5)
os.remove("C:\\Users\\AminataWillane\\Desktop\\selenium_download\\BrowserStack - List of devices to test on.csv")
print("File had been removed successfully")
except FileNotFoundError as er:
print("File Not Found !")
Then once I tried out the same logic while implementing Page Object Model, it isn't working :
here is the related code :
import os
import time
from seleniumpagefactory.Pagefactory import PageFactory
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
from test.test_download import options
class QA_download_me(PageFactory):
def __init__(self, driver):
self.driver = driver
options = driver.ChromeOptions();
prefs = {"download.default_directory": "C:\\Users\\AminataWillane\\Desktop\\selenium_download"}
options.add_experimental_option("prefs", prefs);
driver.Chrome(executable_path='./chromedriver', chrome_options=options)
locators = {
"documentation": ('ID', 'NavLinkDocumentation'),
"sdk": ('ID', 'NavLinkDocumentation1'),
"download_btn": ('XPATH', '//*[#id="server-root"]/div/div/main/div/div/div[2]/div/div[2]/div/div/a[1]'),
}
def download_me(self):
# create a service
self.documentation.click_button()
time.sleep(5)
self.sdk.click_button()
time.sleep(5)
self.download_btn.click_button()
time.sleep(10)
try:
print('try started')
if os.path.isfile(
'C:\\Users\\AminataWillane\\Desktop\\selenium_download\\MyriadSDK-latest.tgz'):
print("File download is completed and the file will be deleted")
time.sleep(5)
os.remove(
"C:\\Users\\AminataWillane\\Desktop\\selenium_download\\MyriadSDK-latest.tgz")
print("File had been removed successfully")
except FileNotFoundError as er:
print("File Not Found !")
Here is the error I've got :
ERROR: test_download_me (test.test_book_2.TestLoginTwo)
down= QA_download_me(self.driver)
File "C:\Users\AminataWillane\PycharmProjects\myProj\src\pages\download_me.py", line 15, in __init__
options = driver.ChromeOptions();
AttributeError: 'WebDriver' object has no attribute 'ChromeOptions'
in the second code snipper, when ur calling ChromeOptions() from driver, driver is only self.driver and hasn't yet been identified as selenium.webdriver.
try adding to imports:
from selenium import webdriver
and then say:
options = webdriver.ChromeOptions()
this also goes for when you instantiate your webdriver later (remove self.driver = driver from beginning of code):
self.driver = webdriver.Chrome(...)
I have a selenium-python automation test, I am generating HTML/JSON reports using Pytest. I want to add response time in the HTML/JSON report, Is this even possible?
following is my code
test_screenshot.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pytest_html
from selenium.common.exceptions import InvalidSessionIdException
def test_Openurl(setup):
driver = setup["driver"]
url = setup["url"]
try:
before_time = datetime.now().strftime('%H%M%S%f') # Timestamp
driver.get(url)
now_time = datetime.now().strftime('%H%M%S%f') # Timestamp
response_time = int(now_time) - int(before_time)
except Exception as e:
print(e.message)
assert driver.current_url == URL
driver.save_screenshot("ss.png")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.save_screenshot("ss1.png")
driver.close()
Conftest.py
import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
def pytest_addoption(parser):
parser.addoption("--url", action="store", default="https://google.com/")
#pytest.fixture()
def setup(pytestconfig):
s = Service("C:/Users/Yash/Downloads/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
yield {"driver":driver, "url": pytestconfig.getoption("url")}
Following is the command i am using to generate reports
pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html=report/report.html test_screenshot.py
you can use some hooks to JSON reports to do this. something along the line of these in your conftest.py file:
#hookimpl(optionalhook=True)
def pytest_json_runtest_metadata(call):
"""
fixture from the pytest-json-report plugin that will add your info
"""
if call.when != 'call':
return {}
# collect the start and finish times in ISO format for the US/Eastern timezone
start_iso_dt = timezone('US/Eastern').localize(datetime.fromtimestamp(call.start)).isoformat()
stop_iso_dt = timezone('US/Eastern').localize(datetime.fromtimestamp(call.stop)).isoformat()
return {'start': start_iso_dt, 'stop': stop_iso_dt
That will land up in your json_report metadata. (my code needed that US/Eastern timezone, you can obviously adjust accordingly, or just do the difference calculation in this function and return {'response_time': mydiff}
I want to use proxy with selenium, I watched the video from youtube how to do it, but it doesn't work. This is my code:
import names
import time as t
import random
import requests
import pyautogui
import smsactivateru
import tkinter as tk
import sys
import socket
from selenium import webdriver
from selenium.webdriver.common.proxy import *
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from smsactivateru import Sms, SmsTypes, SmsService, GetBalance, GetFreeSlots, GetNumber
from fake_useragent import UserAgent
options = webdriver.FirefoxOptions()
# set useragent
ua = UserAgent()
options.set_preference("general.useragent.override",ua.random)
# set proxy
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
proxy = '91.214.31.234:8080'
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy
}
driver = webdriver.Firefox(options=options, proxy=proxy, capabilities=firefox_capabilities)
driver.get('https://2ip.ru')
t.sleep(5)
Why proxy doesn't work? Please help me. Kind people
The following code works for me in chrome.
PROXY = '91.214.31.234:8080'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(options=chrome_options)
chrome.get("https://www.icanhazip.com")
Try this for FireFox.
from selenium import webdriver
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy": PROXY,
"sslProxy": PROXY,
"proxyType": "MANUAL",
}
driver = webdriver.Firefox()
driver.get("https://www.icanhazip.com")
I'm using selenium to make a headless scraping of a website within an endpoint of an API using Flask for Python. I made several tests and my selenium scraping code works perfectly within a script and while running as an API in the localhost. However, when I deploy the code in a remote server, the requests always return a 502 Bad Gateway error. It is weird because by logging I can see that the scraping is working correctly, but the server responds with 502 before the scraping finish processing, as if it was trying to set up a proxy and it fails. I also noticed that removing the time.sleep in my code makes it return a 200 although the result could be wrong because it doesn't give selenium the proper time to load the all the page to scrape.
I also tried to set up to use falcon instead of flask and I get a similar error. This is a sample of my recent code using Falcon:
class GetUrl(object):
def on_get(self, req, resp):
"""
Get Request
:param req:
:param resp:
:return:
"""
# read parameter
req_body = req.bounded_stream.read()
json_data = json.loads(req_body.decode('utf8'))
url = json_data.get("url")
# get the url
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)
driver.get(url)
time.sleep(5)
result = False
# check for outbound links
content = driver.find_elements_by_xpath("//a[#class='_52c6']")
if len(content) > 0:
href = content[0].get_attribute("href")
result = True
driver.quit()
# make the return
return_doc = {"result": result}
resp.body = json.dumps(return_doc, sort_keys=True, indent=2)
resp.content_type = 'text/string'
resp.append_header('Access-Control-Allow-Origin', "*")
resp.status = falcon.HTTP_200
I saw some other similar issues like this, but even though I can see that there is a gunicorn running in my server, I don't have nginx, or at least it is not running where it should running. And I don't think Falcon uses it. So, what exactly am I doing wrong? Some light in this issue is highly appreciated, thank you!
This might work:
from IPython.display import clear_output
import time as time
import json
!apt-get update
!apt install chromium-chromedriver
!which chromedriver
!pip install selenium
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
!pip install page_objects
import page_objects
from page_objects import PageObject, PageElement
time.sleep(1)
clear_output()
class GetUrl(object):
def on_get(self, req, resp):
"""
Get Request
:param req:
:param resp:
:return:
"""
# read parameter
req_body = req.bounded_stream.read()
json_data = json.loads(req_body.decode('utf8'))
url = json_data.get("https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175")
# get the url
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options = options)
driver.implicitly_wait(3)
driver.get("https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175")
result = False
# check for outbound links
contentStorage = []
content = driver.find_elements_by_tag_name('a')
for i in content:
contentStorage.append(i.get_attribute('text'))
result = True
#driver.quit()
# make the return
return_doc = {"result": result}
resp.body = json.dumps(return_doc, sort_keys=True, indent=2)
resp.content_type = 'text/string'
resp.append_header('Access-Control-Allow-Origin', "*")
resp.status = falcon.HTTP_200
However, I was testing it without using a class object, and also it's using Chrome instead of FireFox:
from IPython.display import clear_output
import time as time
!apt-get update
!apt install chromium-chromedriver
!which chromedriver
!pip install selenium
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
!pip install page_objects
import page_objects
from page_objects import PageObject, PageElement
time.sleep(1)
clear_output()
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options = options)
driver.implicitly_wait(3)
driver.get('https://stackoverflow.com/questions/69038958/selenium-flask-falcon-in-python-502-bad-gateway-error/69546175#69546175')
content = driver.find_elements_by_tag_name('a')
contentStorage = []
for i in content:
contentStorage.append(i.get_attribute('text'))
#driver.quit()