Python Selenium 从某些 class 中获取价值
Python Selenium get value from certain class
我正在尝试从此代码中获取 value=" "
中的信息。
只要 class 中有 orange 这个词,我就想存储这个值。
这种情况下的预期结果是在变量中存储值“JKK-LKK”。
<input type="text" readonly="" class="form-control nl-forms-wp-orange" value="JKK-LKK" style="cursor: pointer; border-left-style: none;>
我试过使用
text = driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]").get_attribute("value"))
但我得到:
AttributeError: 'list' Object has no attribute 'get_attribute'.
我也试过 getText("value")
但我得到的不是有效的 Xpath 表达式。
如果我只尝试使用
driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]")
列表变为空。所以我觉得我可能遗漏了其他一些关键部分。
我可能做错了什么?
从包含 [=62= 的元素中打印 value 属性的值,即 JKK-LKK ] 属性为 nl-forms-wp-orange
您可以使用以下任一项 :
使用css_selector
:
print(driver.find_element_by_css_selector("input.nl-forms-wp-orange").get_attribute("value"))
使用xpath
:
print(driver.find_element_by_xpath("//input[contains(@class, 'nl-forms-wp-orange')]").get_attribute("value"))
理想情况下,您需要为 visibility_of_element_located()
引入 ,您可以使用以下任一方法 :
使用CSS_SELECTOR
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.nl-forms-wp-orange"))).get_attribute("value"))
使用XPATH
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]"))).get_attribute("value"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在尝试从此代码中获取 value=" "
中的信息。
只要 class 中有 orange 这个词,我就想存储这个值。
这种情况下的预期结果是在变量中存储值“JKK-LKK”。
<input type="text" readonly="" class="form-control nl-forms-wp-orange" value="JKK-LKK" style="cursor: pointer; border-left-style: none;>
我试过使用
text = driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]").get_attribute("value"))
但我得到:
AttributeError: 'list' Object has no attribute 'get_attribute'.
我也试过 getText("value")
但我得到的不是有效的 Xpath 表达式。
如果我只尝试使用
driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]")
列表变为空。所以我觉得我可能遗漏了其他一些关键部分。 我可能做错了什么?
从包含 [=62= 的元素中打印 value 属性的值,即 JKK-LKK ] 属性为 nl-forms-wp-orange
您可以使用以下任一项
使用
css_selector
:print(driver.find_element_by_css_selector("input.nl-forms-wp-orange").get_attribute("value"))
使用
xpath
:print(driver.find_element_by_xpath("//input[contains(@class, 'nl-forms-wp-orange')]").get_attribute("value"))
理想情况下,您需要为 visibility_of_element_located()
引入
使用
CSS_SELECTOR
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.nl-forms-wp-orange"))).get_attribute("value"))
使用
XPATH
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]"))).get_attribute("value"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC