Python : Selenium 点击账户创建

Python : Selenium click account creation

我必须为住在疗养院的少数“老年人”自动创建帐户。 (免费,我是志愿者) 我正在弄清楚如何点击这样的按钮 :

<button type="submit" data-uid="id-3" role="button" class="_2CEO5 _2eWEL _3cHWt _2Jtx4 V9Kxm _1s9tX _2eWEL _3cHWt _2eWEL _3cHWt _2Jtx4 _3cHWt _5Qrl3">
<span class="_100nC"><div class="_2EteE _2WXqV _34xiU _1TcN5 _2WXqV _34xiU" style="border-top-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255);"></div></span><span class="_2HwQ3">Create an Account</span></button>

我试过 在 chrome 浏览器上,右键单击 > 复制 Xpath

driver.find_element_by_xpath('//*[@id="plex"]/div/div/div/div[1]/form/button')

但是 Selenium 找不到它

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="plex"]/div/div/div/div[1]/form/button"}

如何点击? 感谢任何提示

url是:https://www.plex.tv/sign-up/

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.plex.tv/sign-up/")
driver.maximize_window()

try:
    driver.find_element_by_xpath("/html/body/div[2]/div[3]/a[1]").click()
    driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[4]/div/a").click()
    driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[5]/a").click()
except Exception as e:
    print("Nothing to click before submission!")

iframe_el = driver.find_element(By.ID, "fedauth-iFrame")
# iframe_el.get_attribute("src")


try:
    driver.get(iframe_el.get_attribute("src"))
    # fill the form
    element = (
        WebDriverWait(driver, 10)
        .until(
            EC.presence_of_element_located(
                (By.XPATH, "/html/body/div[1]/div/div/div/div[1]/form/button")
            )
        )
        .click()
    )
    print("button is found")

except Exception as e:
    print("no element")
finally:
    driver.quit()


不是针对父 BUTTON 而是针对内部 SPAN 诱导 WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'][data-uid^='id'] span:nth-of-type(2)"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and starts-with(@data-uid, 'id')]//span[text()='Create an Account']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

参考资料

您可以在以下位置找到关于 的一些相关讨论:

直接导航到他们的登录身份验证 iframe link

from selenium import webdriver


  
driver = webdriver.Chrome(executable_path=path)
driver.implicitly_wait(10)
driver.get('https://www.plex.tv/sign-up/')

#accept cookies
driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/a[2]').click()
#navigate to iframe src
driver.get(driver.find_element_by_id("fedauth-iFrame").get_attribute('src'))
#input email
driver.find_element_by_xpath('//*[@id="email"]').send_keys("email@email.com")
#input password
driver.find_element_by_xpath('//*[@id="password"]').send_keys('password')
#submit
driver.find_element_by_xpath('//*[@id="plex"]/div/div/div/div[1]/form/button/span[2]').click()