Python Selenium Multiple Webdrivers - python

So I have this python selenium code and I want it to run multiple times at the same time. So when I activate it, it opens multiple webdrivers and execute this script at the same time. How can I do this?
driver.get(base_url)
password_id = driver.find_element_by_id('password')
password = input("Password: ")
password_id.send_keys(password)
password_id.send_keys(Keys.ENTER)
email_id1 = EC.presence_of_element_located((By.ID, 'email'))
WebDriverWait(driver, 100).until(email_id1)
email_id = driver.find_element_by_id('email')
email_id.send_keys(user_email)
start = time.time()
print(Fore.WHITE + "STATUS:" + Fore.LIGHTYELLOW_EX + " Email Filled!")
name_id = driver.find_element_by_id('name')
name_id.send_keys(user_name)
print(Fore.WHITE + "STATUS:" + Fore.LIGHTYELLOW_EX + " Name Filled!")
button_id = driver.find_element_by_id('purchase')
end = time.time()
button_id.click()
print(Fore.WHITE + "STATUS:" + Fore.LIGHTGREEN_EX + " Processing order...")
sleep(10)
timeresult = end - start
speed = (str(timeresult))
checkout_done = driver.current_url

Have you tried multithreading?
Code below allows me to open two browsers at once (in separate loops) and control them within functions (first_window(), second_window()).
from selenium import webdriver
from threading import Thread
def first_window():
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://stackoverflow.com/")
def second_window():
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://stackoverflow.com/")
if __name__ == '__main__':
Thread(target=first_window).start()
Thread(target=second_window).start()

Related

Bypass Yahoo reCAPTCHA by 2captcha API

I'm trying to create yahoo account using python selenium, and while creating i have to bypass a recaptcha. I'm using 2Captcha API to automate solving captchas
My issue is i can't solve recaptcha
based on my tests, i noticed that yahoo is using entreprise captcha, not sure if it's a V2 or V3
Here the API Documentation : https://2captcha.com/2captcha-api
here is my code :
import os
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import random
from twocaptcha import TwoCaptcha
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("excludeSwitches", ["enable-logging"])
opt.add_experimental_option("prefs", {
"profile.default_content_setting_values.media_stream_mic": 2,
"profile.default_content_setting_values.media_stream_camera": 2,
"profile.default_content_setting_values.geolocation": 2,
"profile.default_content_setting_values.notifications": 2
})
executable_path = r'chromedriver'
os.environ["webdriver.chrome.driver"] = executable_path
global driver
driver = webdriver.Chrome(r'chromedriver', options=opt)
time.sleep(5)
driver.get("https://login.yahoo.com/account/create")
# Fname and Lname
time.sleep(6)
driver.find_element_by_xpath("//input[#name='firstName']").send_keys("fname")
time.sleep(3)
driver.find_element_by_xpath("//input[#name='lastName']").send_keys("lname")
# Email
time.sleep(3)
numberid = random.randint(100000, 900000)
driver.find_element_by_xpath("//input[#name='yid']").send_keys("fname" + str(numberid) + "lname")
# Password
time.sleep(3)
driver.find_element_by_xpath("//input[#name='password']").send_keys("TestEPWD.")
######## number region +
FC = '(+212)'
option_el = driver.find_element_by_xpath("//option[contains(text(),'%s')]" % FC)
option_el.click()
driver.find_element_by_xpath("//input[#name='phone']").send_keys('684838340')
# Choose date
Month = random.randint(1, 12)
Months = "//option[#value='{}']".format(Month)
monthselect = driver.find_element_by_xpath(Months)
monthselect.click()
time.sleep(3)
Day = random.randint(1, 27)
driver.find_element_by_xpath("//input[#name='dd']").send_keys(Day)
time.sleep(3)
Year = random.randint(1975, 2000)
driver.find_element_by_xpath("//input[#name='yyyy']").send_keys(Year)
time.sleep(3)
list = ["Man", "Woman"]
item = random.choice(list)
driver.find_element_by_xpath("//input[#name='freeformGender']").send_keys(item)
time.sleep(3)
driver.find_element_by_xpath("//button[#name='signup']").click()
time.sleep(5)
# CAPTCHA PART :
api_key = os.getenv('APIKEY_2CAPTCHA', 'mycaptchaAPI')
solver = TwoCaptcha(api_key)
yy = driver.current_url
try:
result = solver.recaptcha(
sitekey="6LeGXAkbAAAAAAMGHQaxwylqpyvtF2jJMrvJff1h",
url=yy,
entreprise=1,
version='v3',
score=0.2
)
except Exception as e:
print(e)
else:
print('result: ' + str(result))

perform() and reset_actions() in ActionChains not working selenium python

This is the code that habe no error:
perform() and reset_actions()
but these two functions have to work combinedly
import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import random
# Setting the chrome_options
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
google_search = [
"1.' driver.switch_to.active_element' ",
"2.this code is a one of important snippet for facebook automation.",
]
random_google_search = random.choice(google_search)
# Setting the Chrome Driver
global driver
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)
# Setting the Actions
global actions
actions = ActionChains(driver)
#the loop Running
def navigation():
time.sleep(5)
actions.reset_actions()
driver.get("https://google.com")
actions.send_keys(random_profile_post)
total_tab = 3
sleep_time = 1
implicitly_wait_time = 4
actions.reset_actions()
driver.implicitly_wait(implicitly_wait_time)
time.sleep(sleep_time)
for i in range(total_tab):
actions.send_keys(Keys.TAB)
print("Pressing * " + str(i + 1) + " * No Tab")
actions.send_keys(Keys.ENTER)
actions.perform()
for i in range(10):
navigation()
print("Pressing * " + str(i + 1) + " * st navigation function")
I am working with navigation() functions:
in the loop area
actions.send_keys(Keys.TAB)
actions.reset_actions()
I need to reset action but it's not reseating previous preform()
What will be the batter way to do that.
Please watch the youtube video for more clear understanding.
The issue is fixed already but will be in the next releases. Check the issue #6837 in the github.
For now you can use temporary solution.
def perform_actions():
""" Perform and reset actions """
actions.perform()
actions.reset_actions()
for device in actions.w3c_actions.devices:
device.clear_actions()
# the loop Running
def navigation():
time.sleep(5)
driver.get("https://google.com")
actions.send_keys("A")
total_tab = 4
sleep_time = 1
implicitly_wait_time = 4
# actions.reset_actions()
driver.implicitly_wait(implicitly_wait_time)
time.sleep(sleep_time)
for i in range(total_tab):
actions.send_keys(Keys.TAB)
print("Pressing * " + str(i + 1) + " * No Tab")
actions.send_keys(Keys.ENTER)
perform_actions()
print()
You can just instantiate it inside the function. If this does not work you can try to throw it all inside def, aside from the global lines, which should be deleted.
#the loop Running
def navigation():
actions = ActionChains(driver)
.
.
.
You should store ActionChains in a variable, so you can store you action in a object.
def navigateGroupPostBtn():
navigateGroupJoinBtnActions = ActionChains(driver)
total_tab = 23
sleepTime = 1
implicitlyWaitTime = 20
time.sleep(sleepTime)
for i in range(total_tab):
driver.implicitly_wait(implicitlyWaitTime)
navigateGroupJoinBtnActions.send_keys(Keys.TAB)
print("Pressing * " + str(i + 1) + " * No Tab")
navigateGroupJoinBtnActions.send_keys(Keys.ENTER)
navigateGroupJoinBtnActions.perform_actions()
print("Navigate Groum Join Btn Successfully ")
navigateGroupJoinBtnActions = ActionChains(driver)

My Python script can't access to internet using Selenium

I am creating a script that is concerned with accessing the internet, but it doesn't work. My connection to the internet is stable and the rest of the script works perfectly. I've used selenium module to do so.
Please look into the script I've attached and let me know if there are any problems and how I could resolve those.
def search_web(input):
driver = webdriver.Chrome()
driver.implicitly_wait(1)
driver.maximize_window()
if 'youtube' in input.lower():
assistant_speaks("Opening in youtube")
indx = input.lower().split().index('youtube')
query = input.split()[indx + 1:]
driver.get("http://www.youtube.com/results?search_query =" + '+'.join(query))
return
elif 'wikipedia' in input.lower():
assistant_speaks("Opening Wikipedia")
indx = input.lower().split().index('wikipedia')
query = input.split()[indx + 1:]
driver.get("https://en.wikipedia.org/wiki/" + '_'.join(query))
return
else:
if 'google' in input:
indx = input.lower().split().index('google')
query = input.split()[indx + 1:]
driver.get("https://www.google.com/search?q =" + '+'.join(query))
elif 'search' in input:
indx = input.lower().split().index('google')
query = input.split()[indx + 1:]
driver.get("https://www.google.com/search?q =" + '+'.join(query))
else:
driver.get("https://www.google.com/search?q =" + '+'.join(input.split()))
return
Try adding executable_path to chrome driver. I ran with executable_path, it's working fine for me.
driver = webdriver.Chrome(executable_path='path_of_chrome_driver')

How do I export data from multiple pages into a csv file?

I am working on a scraping project, and am in the final stages. Right now, my code can navigate to the first profile, scrape the data from that profile, print that data, then move on to the next profile, and repeat the process. Now, I want to put the data I collect into a csv file instead of printing it. I am not sure how to do this, so I am looking for guidance/updates to my current code. Thank you for your help!
My current code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome("/Users/nzalle/Downloads/chromedriver")
driver.get("https://directory.bcsp.org/")
count = int(input("Number of Profiles to Scrape: "))
body = driver.find_element_by_xpath("//body")
profile_count = driver.find_elements_by_xpath("//div[#align='right']/a")
while len(profile_count) < count: # Get links up to "count"
body.send_keys(Keys.END)
sleep(1)
profile_count = driver.find_elements_by_xpath("//div[#align='right']/a")
for link in profile_count: # Calling up links
temp = link.get_attribute('href') # temp for
driver.execute_script("window.open('');") # open new tab
driver.switch_to.window(driver.window_handles[1]) # focus new tab
driver.get(temp)
# scrape code
Name = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr/td[5]/div/table[1]/tbody/tr/td[1]/div[2]/div').text
IssuedBy = "Board of Certified Safety Professionals"
CertificationorDesignaationNumber = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr/td[5]/div/table[1]/tbody/tr/td[3]/table/tbody/tr[1]/td[3]/div[2]').text
CertfiedorDesignatedSince = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr/td[5]/div/table[1]/tbody/tr/td[3]/table/tbody/tr[3]/td[1]/div[2]').text
try:
AccreditedBy = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr/td[5]/div/table[1]/tbody/tr/td[3]/table/tbody/tr[5]/td[3]/div[2]/a').text
except NoSuchElementException:
AccreditedBy = "N/A"
try:
Expires = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table/tbody/tr/td[5]/div/table[1]/tbody/tr/td[3]/table/tbody/tr[5]/td[1]/div[2]').text
except NoSuchElementException:
Expires = "N/A"
Data = (Name + " , " + IssuedBy + " , " + CertificationorDesignaationNumber + " , " + CertfiedorDesignatedSince + " , " + AccreditedBy + " , " + Expires)
print(Data)
driver.close()
driver.switch_to.window(driver.window_handles[0])
driver.close()

How to program the code to run for a specific time and exit after that?

This is my piece of code. I want the program to run for just 10 seconds and if it exceeds 10 secs, the program should stop. For that purpose, I used while loop as shown. But it is not working. Please help me with some alternative or an enhancement in the same code. Thanks.
def second_func():
first_id = driver.find_element_by_xpath('//*[#id="instant"]')
first_id.click()
ids = driver.find_element_by_xpath(
'//*[#id="R629789109123908870"]/div[2]/table[1]/tbody/tr[11]/td[3]/a')
ids.click()
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
ans = driver.find_elements_by_xpath('//*[#id="P15_ENVIRONMENT_AAI_PASSWORD"]')
for i in ans:
current_password = i.text
with open('data/password', 'r') as file:
for line in file:
previous_password = line
if current_password == previous_password:
# email_sender(current_password)
print(current_password)
else:
with open('data/password', 'w') as file:
file.write(current_password)
file.close()
print(current_password)
# email_sender(current_password)
driver.quit()
options = webdriver.ChromeOptions()
options.add_argument('--disable-extentions')
options.add_argument('--enable-popup-blocking')
options.add_argument('--start-maximized')
driver = webdriver.Chrome(executable_path='C:\\Users\\hp\\Downloads\\chromedriver',
chrome_options=options)
end_time = time.time() + 10
while time.time() < end_time:
driver.get("http://demo.oracle.com")
login = driver.find_element_by_xpath('//*[#id="sso_username"]')
login.send_keys('username')
password = driver.find_element_by_xpath('//*[#id="ssopassword"]')
password.send_keys('password')
login_click = driver.find_element_by_xpath(
'/html/body/div/div[3]/div[1]/form/div[2]/span/input')
login_click.click()
else:
driver.quit()

Categories

Resources