以下是HTML结构:
<div class='list'> <div> <p class='code'>12345</p> <p class='name'>abc</p> </div> <div> <p class='code'>23456</p> <p class='name'>bcd</p> </div> </div>
并且有一个config.py供用户输入。如果用户在config.code中输入23456,Selenium python如何选择第二个对象?我find_by_css_selector()用来查找和选择对象,但是只能选择第一个对象,即 Code =‘12345’ 。我尝试使用find_by_link_text(),但这是<p>元素而不是<a>元素。任何人都可以帮助.....
find_by_css_selector()
find_by_link_text()
<p>
<a>
要根据用户使用Selenium和python的输入来定位元素,您需要为引入WebDriverWait,visibility_of_element_located()并且您可以使用以下两种定位器策略之一:
visibility_of_element_located()
XPATH
user_input = '23456' element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='list']//div/p[@class='code' and text()='" +user_input+ "']")))
%s
user_input = '23456' element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='list']//div/p[@class='code' and text()='%s']"% str(user_input))))
format()
user_input = '23456' element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='list']//div/p[@class='code' and text()='{}']".format(str(user_input)))))
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC