Search code examples
javascriptpython-3.xseleniumxpathiframe

How to bypass human verification 'press and hold' using Selenium in Python?


I am trying to scrape some product reviews using Selenium and Python from this site but it connects another site and shows a popup at any point randomly, where I need to press and hold the button for human verification.

I am using chrome web driver and tried to solve it, to get the path using driver.find_element_by_xpath and many other ways. I also found that the 'Press and Hold' button is inside an iframe and so tried to switch to the iframe by driver.switch_to_frame('//iframe') or driver.switch_to_frame(0) but I failed. I can't find any iframe name or id to take any action.

Is there any way to bypass or take action (pressing and holding the button) whenever it occurs (as a site or popup) and also dismiss other popups during the process using selenium and python? any suggestions would be greatly appreciated!


Solution

  • Jacob's solution from this issue here solves this problem.

    import time
    from selenium import webdriver as wd
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver = wd.Chrome('./web driver/chromedriver.exe')
    target_url = 'https://www.walmart.com/blocked?url=L2lwL0Nsb3JveC1EaXNpbmZlY3RpbmctV2lwZXMtMjI1LUNvdW50LVZhbHVlLVBhY2stQ3Jpc3AtTGVtb24tYW5kLUZyZXNoLVNjZW50LTMtUGFjay03NS1Db3VudC1FYWNoLzE0ODk4MzY1&uuid=9ed7f800-f288-11eb-ad50-1b3c9c7d7310&vid=9cf07351-f288-11eb-9ab5-ef26d206453b&g=b'
    driver.get(target_url)
    driver.maximize_window()
    
    element = driver.find_element_by_css_selector('#px-captcha')
    action = ActionChains(driver)
    action.click_and_hold(element)
    action.perform()
    time.sleep(10)
    action.release(element)
    action.perform()
    time.sleep(0.2)
    action.release(element)