#How to use keyboards actions with multithreading seleniumbase ?

1 messages · Page 1 of 1 (latest)

sudden eagle
#

I'm using SeleniumBase with ThreadPoolExecutor for multithreading. How can I properly use keyboard actions (like pressing Enter, Tab, etc.) in each thread without interference?

class SBDriver:
@classmethod
def fetch_content(self, url: str, proxy: Optional[str] = None) -> str:
content = ''
try:
with SB(
uc=True,
browser='chrome',
proxy=proxy,
xvfb=True,
) as driver:
driver.uc_open_with_reconnect(url)
driver.uc_gui_press_keys = True
self.resolve(driver)
content = driver.get_page_source()
except Exception as err:
logging.error(err)
return content

@staticmethod
def resolve(driver: BaseCase):
    try:
        driver.disconnect()

        pyautogui.press('enter')
        time.sleep(2)

        pyautogui.press('tab')
        time.sleep(2)

        pyautogui.keyDown('enter')
        time.sleep(10)
        pyautogui.keyUp('enter')
        time.sleep(5)

        driver.connect()
    except Exception as err:
        logging.error(err)

def fetch_with_proxy(url, proxy):
    return SBDriver().fetch_content(url, proxy)


with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    results = list(executor.map(fetch_with_proxy, urls_d, proxies))
warm root
#

Using PyAutoGUI directly only works with the top browser. (Not great for multithreading when you have a lot of keyboard actions.) You may have to run single-threaded in your case. Or spread out the work across multiple machines, such as multiple GitHub Actions jobs running at the same time. Eg. https://github.com/mdmintz/undetected-testing/actions

GitHub

Find out if SeleniumBase can bypass CAPTCHAs & bot-detection services. - Workflow runs · mdmintz/undetected-testing

sudden eagle
# warm root Using PyAutoGUI directly only works with the top browser. (Not great for multith...

In undetected_driver and Selenium, I used ActionChains. But is it possible to use ActionChains in SeleniumBase?
def solve_permiterx(driver: Chrome):
try:
action = ActionChains(driver)
action.send_keys(Keys.ENTER)
action.pause(2)
action.send_keys(Keys.TAB)
action.pause(2)
action.key_down(Keys.ENTER)
action.pause(10)
action.key_up(Keys.ENTER)
action.perform()
time.sleep(5)
return True
except Exception as err:
return False

warm root
#

Any Selenium functionality (such as ActionChains) can be used with SeleniumBase. It's not going to be stealthy though.

sudden eagle
warm root
#

But UC Mode does have everything you need without it.