在HTML标签下填写输入框

2024-09-27 09:35:21 发布

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

我正在尝试使用Python登录到一个网页。但是,我没有得到正确的回答。输入框位于web中的HTML表下。你知道吗

这是我在网上查了一些教程后尝试的。你知道吗

driver.find_element_by_xpath("//table[5]/tbody/tr[1]/td[2]/input").send_keys("hi")

HTML代码:

<html>

<head>...</head>

<body>

  <form name="form" action method="post" onsubmit>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table border="0" , width="100%" cellpadding="0" cellspacing="0">...</table>
    <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">...</table>

    <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">
      <tbody>
        <tr align="left">
          <td class="mandatory"> USER ID</td>
          <td class="normal">
            <input class="subject" type="text" name="username" size="35" maxlength="10" onkeypress="navigate();">
          </td>
        </tr>
      </tbody>
    </table>

我感兴趣的是挑选input class = "subject"处的元素,这是第五个表。你知道吗

以下是从Chrome检查器复制的xPath代码:

/html/body/form/table[5]/tbody/tr[1]/td[2]/input

Tags: 代码forminputhtmltablewidthtrclass
2条回答

要将字符序列发送到与文本用户ID相关联的login<input>元素,必须诱导WebDriverWait,使元素可单击,并且可以使用以下任一方法解决方案:可以使用下列Locator Strategies之一:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.subject[name='username'][onkeypress^='navigate']"))).send_keys("Wen Jiaxin")
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='subject' and @name='username'][starts-with(@onkeypress, 'navigate')]"))).send_keys("Wen Jiaxin")
    
  • 注意:必须添加以下导入:

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

下面是您可以使用的xpath。你知道吗

 //form[@name='form']/table[5]//input[@name='username']

enter image description here

您还可以使用屏幕截图中显示的其他xpath。你知道吗

enter image description here

相关问题 更多 >

    热门问题