如何使用Selenium和Python将文本发送到Twitter登录页面中的电子邮件字段

2024-06-25 22:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

用户名文本区域的代码如下所示。 它没有任何id,xpath也不能正常工作

Twitter中的元素如下所示

<input autocapitalize="none" autocomplete="on" autocorrect="off" name="session[username_or_email]" spellcheck="false" type="text" dir="auto" data-focusable="true" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-1inuy60 r-utggzx r-vmopo1 r-1w50u8q r-1swcuj1 r-1dz5y72 r-1ttztb7 r-13qz1uu" value="">

Tags: 代码文本noneid区域元素inputon
2条回答

要将字符序列发送到Twitter登录页面https://twitter.com/login中的电子邮件字段,您必须为element_to_be_clickable()诱导WebDriverWait,并且您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session[username_or_email]']"))).send_keys("zeecode")
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='session[username_or_email]']"))).send_keys("zeecode")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
//input[@name="session[username_or_email]" and @type="text"]

相关问题 更多 >