简体   繁体   English

尝试通过 selenium python 输入框 select 但每次我重新加载页面时元素中的“id”都会更改

[英]Trying to select an input box through selenium python but the 'id' in the element changes every time I reload the page

HTML: HTML:

<input id="4432e17d-eed6-4620-9bb4-d75ffbd321fa" type="email" placeholder="Email address" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">

Refreshed:刷新:

<input id="4a463943-7f58-42ea-9317-ba9f9518811e" type="email" placeholder="Email address" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">

How can I select this element without using ID.我如何在不使用 ID 的情况下 select 这个元素。

I've tried by XPATH and CSS SELECTOR both haven't worked.我试过 XPATH 和 CSS SELECTOR 都没有工作。 Full XPATH:完整的 XPATH:

/html/body/div[2]/div[3]/div[5]/form/div[1]/input

XPATH: XPATH:

//*[@id="4a463943-7f58-42ea-9317-ba9f9518811e" except the ID changes. 

Any help?有什么帮助吗?

To send a character sequence to the Email address field you can use either of the following Locator Strategies :要将字符序列发送到Email 地址字段,您可以使用以下任一定位器策略

  • Using css_selector :使用css_selector

     driver.find_element(By.CSS_SELECTOR, "input[name='emailAddress'][data-componentname='emailAddress']").send_keys("sebtheoo@stackoverflow.com")
  • Using xpath :使用xpath

     driver.find_element(By.XPATH, "//input[@name='emailAddress' and @data-componentname='emailAddress']").send_keys("sebtheoo@stackoverflow.com")

The desired element is a dynamic element, so ideally, to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :所需的元素是动态元素,因此理想情况下,要将字符序列发送到您需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='emailAddress'][data-componentname='emailAddress']"))).send_keys("sebtheoo@stackoverflow.com")
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='emailAddress' and @data-componentname='emailAddress']"))).send_keys("sebtheoo@stackoverflow.com")
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

Try selecting element by Tag name尝试按标签名称选择元素

input_box = driver.find_elements_by_tag_name('tag_name_of_input_box')

or since it has a name attribute so you can try that as well或者因为它有一个 name 属性所以你也可以试试

input_box = driver.find_element_by_name('emailAddress')

or as it also has a place holder so even you can combine the xpath with placeholder或者因为它也有一个占位符,所以即使您可以将 xpath 与占位符结合使用

input_box = driver.find_element_by_xpath('"//input[@placeholder='Email address']"')

You need to check for tags that don't change.您需要检查没有更改的标签。 For example, in both the data, name is not changing.例如,在这两个数据中,名称都没有改变。 Hence you can move ahead with that.因此,您可以继续前进。

To access element using name tag, try this syntax :要使用名称标签访问元素,请尝试以下语法

driver.findElement(By.name("emailAddress"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM