I want to open several browsers at the same time to perform certain actions at the same time ...
But it performs the action only in the first browser and the 2 does not perform any actions.
What can I do to have both running at the same time?
def selenium_test(profil):
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument(r"user-data-dir=C:\\Users\\name\\AppData\\Local\\Google\\Chrome\\User Data")
chromeOptions.add_argument(r'--profile-directory='+profil)
driver = webdriver.Chrome(options=chromeOptions)
return driver
class BOT:
def __init__(self,selenium_test):
self.driver = selenium_test
def url(self):
driver = self.driver
driver.get("https://www.youtube.com/watch?v=R4P7BSFH_oE")
if __name__ == '__main__':
a = BOT(selenium_test(profil="Profile 2"))
thread1 = a.url()
b = BOT(selenium_test(profil="Profile 3"))
thread2 = b.url()
thread1.start()
thread2.start()
Related
When I run below Program, Code "option2[opt2].click()" work well.
But second "option2[opt2].click()" occur "StaleElementReferenceException"
I tried to solve this problem with "time.sleep(), implicitly_wait(), WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, {Xpath}).
But Doesn't work.
What is the problem........
#If you run my program, you can understand my code easily. I am Beginer in Programming and Korean. If explanation is not enough... sorry :( Please help me........
from selenium import webdriver
import pandas as pd
import time
import random
options = webdriver.ChromeOptions()
options.add_argument("user-agent={myagent}")
browser = webdriver.Chrome(options = options)
browser.get("https://yeyak.seoul.go.kr/web/main.do") #첫페이지로
browser.maximize_window()
#노원구 공간시설, 문화체험, 교육강좌 카테고리 크롤링
#column 이름
all_column_name = ['구', '카테고리', '대상', '장소', '이용기간', '접수기간', '선별방법',
'모집정원', '신청제한', '취소기간', '이용요금', '예약방법', '문의전화']
all_data = []
#노원구 선택(이미 Click되어있는데, 또 Click을 하면 오류가 남)
browser.find_element_by_xpath("//*[#id=\"sch_loc\"]/option[11]").click()
time.sleep(1)
for i in range(2,5):
#공간시설, 문화체험, 교육강좌 카테고리 선택
browser.find_element_by_xpath(f"//*[#id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").click()
time.sleep(1)
category_name = browser.find_element_by_xpath(f"//*[#id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").text
district = browser.find_element_by_xpath("//*[#id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[1]/select/option[11]").text #구 이름 district 저장
#시설 선택 버튼_소주제
button2 = browser.find_element_by_xpath("//*[#id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[2]/select")
button2.find_elements_by_tag_name("option")
option2 = button2.find_elements_by_tag_name("option")
if len(option2) == 1: #만약 옵션이 별도로 없는 경우 다음 카테고리로 넘어가기
continue
for opt2 in range(1, len(option2)+1):
print(option2[opt2].text)
option2[opt2].click()
time.sleep(1)
#시설 상세 선택 버튼
button3 = browser.find_element_by_xpath("//*[#id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[3]/select")
option3 = button3.find_elements_by_tag_name("option")
if len(option3) == 1: #만약 옵션이 별도로 없는 경우 다음 카테고리로 넘어가기
continue
for opt3 in range(1, len(option3)+1):
small_data = []
option3[opt3].click()
time.sleep(1)
#예약하기 버튼 클릭
browser.find_element_by_xpath("//*[#id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/button").click()
time.sleep(1)
#테이블에서 데이터 가져오기
table = browser.find_element_by_xpath("//*[#id=\"aform\"]/div[1]/div[2]/ul")
rows = table.find_elements_by_tag_name("li")
small_data.append(district) # 구 이름 넣기
small_data.append(category_name) # 카테고리 이름 넣기
for row in rows:
small_data.append(row.text.split("\n")[1])
all_data.append(small_data)
time.sleep(random.uniform(2,3))
browser.get("https://yeyak.seoul.go.kr/web/main.do")
time.sleep(random.uniform(2,3))
browser.find_element_by_xpath("//*[#id=\"sch_loc\"]/option[11]").click() #노원구 클릭
time.sleep(random.uniform(2,3))
browser.find_element_by_xpath(f"//*[#id='contents']/div[1]/div[1]/div/div/div[1]/ul/li[{i}]").click() #카테고리 클릭
time.sleep(random.uniform(2,3))
browser.find_element_by_xpath(f"//*[#id=\"contents\"]/div[1]/div[1]/div/div/div[2]/div[3]/div[2]/select/option[{opt2}]")
time.sleep(random.uniform(2,3))
if opt3 == len(option3)+1:
break
This question already has answers here:
WebDriverException: Message: 'chromedriver' executable needs to be in PATH while setting UserAgent through Selenium Chromedriver python
(1 answer)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome
(1 answer)
Closed 1 year ago.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import ElementNotInteractableException, NoSuchElementException, StaleElementReferenceException
from random import randint, randrange
#from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
import random
BESTBUY_URL = 'https://www.bestbuy.com/site/sony-playstation-5-digital-edition-console/6430161.p?skuId=6430161'
BESTBUY_TEST_URL = 'https://www.bestbuy.com/site/sony-playstation-5-dualsense-wireless-controller-cosmic-red/6464309.p?skuId=6464309'
WAIT_TIME = 7
PRICE_LIMIT = 500
class Zac:
def __init__(self, username, password):
self.username = username
self.password = password
#binary = FirefoxBinary("/Applications/Firefox.app/Contents/MacOS/firefox-bin")
#self.driver = webdriver.Firefox("Applications/Firefox.app")
driver = webdriver.Chrome(executable_path="Applications/Google Chrome.app")
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
def signIn(self):
driver = self.driver
##Username
username_elem = driver.find_element_by_xpath("_____")
username_elem.clear()
username_elem.send_keys(self.username)
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
username_elem.send_keys(Keys.RETURN)
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
##Password
password_elem = driver.find_element_by_xpath("_____")
password_elem.clear()
password_elem.send_keys(self.password)
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
password_elem.send_keys(Keys.RETURN)
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
def findProduct(self):
driver = self.driver
driver.get(BESTBUY_TEST_URL)
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
isAvailable = self.isProductAvailable()
if isAvailable == 'Sold Out':
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
self.findProduct()
elif isAvailable <= PRICE_LIMIT:
buy_now = driver.find_by_name('submit.buy-now')
buy_now.click()
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
self.signIn()
##Place Order
place_order = driver.find_element_by_name('placeYourOrder1').text
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
## place_order.click()
## time.sleep(randict(int(WAIT_TIME/2), WAIT_TIME))
else:
time.sleep(randint(int(WAIT_TIME/2), WAIT_TIME))
self.findProduct()
def isProductAvailable(self):
driver = self.driver
available = driver.find_element_by_class_name('a-color-price').text
if available == 'Currently unavailable.':
print(f'***** AVAILABLE: {available}')
return available
else:
print(f'***** PRICE: {available}')
return float(available[1:]) ## $123.22 -> 123. 22
def closeBrowser(self):
self.driver.close()
if __name__ == '__main__':
shopBot = Zac(username="_____", password="_____")
shopBot.findProduct()
shopBot.closeBrowser()
For some reason, it is saying that Google Chrome.app executable needs to be in PATH. I am trying to make a scalping bot, and this seems to be my biggest mistake area. I am open to using any browser that is easiest. As you can see in the code I first tried this with FireFox. If anybody could help that would be greatly appreciated.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
path_to_download = '/home/dev/'
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": path_to_download,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
options.add_argument('start-maximized')
driver = webdriver.Chrome(executable_path='/home/dev/Downloads/chromedriver_linux64/chromedriver',
options=options)
#long logic
elem2 = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Excel')]")))
elem2.click() #downloads the file (export to excel)
for now I am stuck with putting a time.sleep(5) then os.rename(f'{path_to_download}/exported.xlsx',f'{path_to_download}/{my_id}.xlsx')
is there a way of controlling the filename while or before the file is downloaded?
You can achieve that by checking for the file existence in your download directory and loop over and over until the file appears.
You can do someting like that :
# check the size of the file and return 0 if doesn't exist
def getSize(filename):
if os.path.isfile(filename):
st = os.stat(filename)
return st.st_size
else:
return 0
def wait_download(file_path):
current_size = getSize(file_path)
printed = False
start_time = time.time()
# loop over and over until the current_size change
while current_size !=getSize(file_path) or getSize(file_path)==0:
current_size = getSize(file_path)
# print something to the string to know that we are waiting for the file
while printed == False:
print("Downloading file...")
print("Waiting for download to complete...")
printed = True
# Here we add an exit to not loop forever
if (time.time() - start_time) > 15:
return -1
return 0
# In your code you can call the function like that
files = wait_download(f'{path_to_download}/exported.xlsx')
if files == 0:
#do something
else:
# the file didn't download
I'm trying to use the following xpath for this page but it is not loading correctly.
groups = ".//*[contains(#class, 'sl-CouponParticipantWithBookCloses_Name ')]"
xp_bp1 = ".//following::div[contains(#class,'sl-MarketCouponValuesExplicit33')][./div[contains(#class,'gl-MarketColumnHeader')][.='1']]//span[#class='gl-ParticipantOddsOnly_Odds']"
The output currently is..
[['3.00'], ['3.00'], ['3.00'] etc,,
Desired:
[['3.00'], ['1.30'], ['1.25'] etc,,
Data I am after
Script:
import csv
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get('https://www.bet365.com.au/#/AC/B1/C1/D13/E108/F16/S1/')
driver.get('https://www.bet365.com.au/#/AC/B1/C1/D13/E108/F16/S1/')
time.sleep(10)
groups = ".//*[contains(#class, 'sl-CouponParticipantWithBookCloses_Name ')]"
#//div[contains(#class, 'gl-ParticipantOddsOnlyDarker gl-ParticipantOddsOnly gl-Participant_General sl-MarketCouponAdvancedBase_LastChild ')]
xp_bp1 = ".//following::div[contains(#class,'sl-MarketCouponValuesExplicit33')][./div[contains(#class,'gl-MarketColumnHeader')][.='1']]//span[#class='gl-ParticipantOddsOnly_Odds']"
while True:
try:
time.sleep(2)
data = []
for elem in driver.find_elements_by_xpath(groups):
try:
bp1 = elem.find_element_by_xpath(xp_bp1).text
except:
bp1 = None
url1 = driver.current_url
data.append([bp1])
print(data)
url1 = driver.current_url
with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
writer = csv.writer(outfile)
for row in data:
writer.writerow(row + [url1])
except TimeoutException as ex:
pass
except NoSuchElementException as ex:
print(ex)
break
I need to open every new iteration, new browser windows, when I try this code:
class GoogleTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Chrome('/home/andrew/Downloads/chromedriver')
self.wait = WebDriverWait(self.browser, 20)
self.browser.implicitly_wait(30)
def test_01_goole(self):
for i in range(3):
browser = self.browser
browser.get('http://www.google.com')
browser.close()
I receive:
WebDriverException: Message: no such session