如何使用硒在网页上查找所有具有特定文本的输入元素
How to find all input elements with specific text on webpage with selenium
基本上我想做的是找到网站上包含特定单词的所有输入元素,并尝试在那里输入文本。例如这个网站 https://www.missyempire.com/customer/account/create/ 有 2 个包含关键字“email”的输入元素。我知道如何指定一个但找不到所有,任何帮助都会很棒。
您可以使用 selenium find_elements
找到所有元素。当您为您的 find_elements
提供定位器时,请创建该定位器 parametrized
并将您的 input text
传递给该定位器。
例如:
text = 'email' -> This is your specific word
testEmails = ['123@example.com', '456@example.com'] -> These are your sample input values
PATH = r"your chromedriver exe path"
driver = webdriver.Chrome(PATH)
driver.get("https://www.missyempire.com/customer/account/create/ ")
driver.maximize_window()
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.NAME, "firstname")))
allEmails = driver.find_elements(By.XPATH, f'//input[@name="{text}"]') -> This is your parametrized xpath locator which will receive your specific word
for email in allEmails:
email.send_keys(random.choice(testEmails)) -> Select your input values random ally
sleep(20) -> This sleep is viewing the entered values before closing the browser
driver.quit()
输出:
基本上我想做的是找到网站上包含特定单词的所有输入元素,并尝试在那里输入文本。例如这个网站 https://www.missyempire.com/customer/account/create/ 有 2 个包含关键字“email”的输入元素。我知道如何指定一个但找不到所有,任何帮助都会很棒。
您可以使用 selenium find_elements
找到所有元素。当您为您的 find_elements
提供定位器时,请创建该定位器 parametrized
并将您的 input text
传递给该定位器。
例如:
text = 'email' -> This is your specific word
testEmails = ['123@example.com', '456@example.com'] -> These are your sample input values
PATH = r"your chromedriver exe path"
driver = webdriver.Chrome(PATH)
driver.get("https://www.missyempire.com/customer/account/create/ ")
driver.maximize_window()
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.NAME, "firstname")))
allEmails = driver.find_elements(By.XPATH, f'//input[@name="{text}"]') -> This is your parametrized xpath locator which will receive your specific word
for email in allEmails:
email.send_keys(random.choice(testEmails)) -> Select your input values random ally
sleep(20) -> This sleep is viewing the entered values before closing the browser
driver.quit()
输出: