代码之家  ›  专栏  ›  技术社区  ›  Pedro Nogueira

如何在使用Selenium的页面上单击代码中的“隐藏”按钮?

  •  0
  • Pedro Nogueira  · 技术社区  · 2 年前

    我正试图点击本页面中的“输入”按钮: https://conta.stone.com.br/login

    但无论是使用这些类:

    "sc-AxhUy hclPzu"

    "sc-AxhCb jYjZpM"

    也不是XPaths:

    "//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span"

    "//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button"

    这里的代码目前看起来像这样:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    option = webdriver.ChromeOptions()
    browser = webdriver.Chrome(options=option, executable_path=config.CHROME_PATH)
    
    login = "https://conta.stone.com.br/login"
    
    
    browser.get(login)
    username = browser.find_element(by=By.XPATH, value='//*[@id="username"]')
    password = browser.find_element(by=By.XPATH, value='//*[@id="password"]')
    
    
    username.send_keys(email)
    password.send_keys(password)
    
    #browser.find_element_by_xpath('//*[@id="recaptcha-anchor"]/div[1]').click()
    WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")))
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='recaptcha-checkbox-border']"))).click()
    
    
    WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button')))
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div[1]/form/div[1]/div[3]/span/span/button'))).click()
    

    我现在到底做错了什么?我已经尝试了我能想到的大多数方法来找到元素,但没有骰子,如果有人能提出任何建议,那就太好了,一切都很好,直到这个按钮,甚至大部分时间都是recaptcha通过。

    我试着用chrome录制这个动作,它给了我以下动作:

    {
        const targetPage = page;
        const element = await waitForSelectors([["aria/Entrar","aria/[role=\"generic\"]"],["#__next > div.clean-page-container__Container-sc-fzay79-0.dGAlqK > form > div.sc-fzoant.MgHsy.card__Card-sc-2xb3kk-0.vMIBG > div.sc-fznLPX.fGbfOF > span > span > button > span"]], targetPage, { timeout, visible: true });
        await scrollIntoViewIfNeeded(element, timeout);
        await element.click({ offset: { x: 25.15625, y: 12.828125} });
    }
    

    我曾尝试在selenium中使用“execute_script”运行它,但由于targetPage没有在Python中定义,所以它没有真正起作用。

    1 回复  |  直到 2 年前
        1
  •  1
  •   Pedro Nogueira    2 年前

    我找到了答案,问题被困在iframe中,以解决recaptcha。

    放置浏览器。切换到。在点击按钮之前,使用默认的内容。

        2
  •  1
  •   Software Tester at ExpandCart    2 年前

    (更新) -它对我有用,请检查:

    (Python)

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    option = webdriver.ChromeOptions()
    browser = webdriver.Chrome(options=option, executable_path=config.CHROME_PATH)
    
    login = "https://conta.stone.com.br/login"
    
    browser.get(login)
    username = browser.find_element(by=By.XPATH, value='//*[@id="username"]')
    password = browser.find_element(by=By.XPATH, value='//*[@id="password"]')
    
    username.send_keys(email)
    password.send_keys(password)
    
    browser.find_element_by_xpath('(//BUTTON[@shape='regular'])[1]').click()
    
    

    (爪哇)

    WebDriver driver;
    WebDriverManager.chromedriver().setup();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-infobars");
    driver = new ChromeDriver(options);
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30, 1));
    driver.manage().window().maximize();
    driver.get("https://conta.stone.com.br/login");
             
    By Btn = By.xpath("(//BUTTON[@shape='regular'])[1]");
    By Email = By.xpath("//*[@id='username']");
    driver.findElement(Email).sendKeys("asdasdasd");
    By Pass = By.xpath("//*[@id='password']");
    driver.findElement(Pass).sendKeys("eefdsfwewrwer123");
    
    driver.findElement(Btn).click();