Selenium get_attribute() 返回的值始终为 1
Value being returned by Selenium get_attribute() is always 1
我正在做一些业余的网络抓取,但在通过 xpath 获取值时遇到了问题。我的目标是 https://kek.tools/t/0x4e15361fd6b4bb609fa63c81a2be19d873717870。我的代码如下所示:
def scrape_price():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
driver.get('https://kek.tools/t/0x841fad6eae12c286d1fd18d1d525dffa75c7effe')
driver.implicitly_wait(100)
price = driver.find_element_by_xpath('//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]')
print(price.get_attribute("value"))
driver.quit()
在检查元素中,//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]
过滤似乎得到了我需要的准确输入字段,但是当我打印值时总是返回“1”。 WebElement
我使用的方法不对吗?
而不是 implicitly_wait
尝试使用 expected_conditions
,如下所示:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def scrape_price():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
wait = WebDriverWait(driver, 20)
driver.get('https://kek.tools/t/0x841fad6eae12c286d1fd18d1d525dffa75c7effe')
price = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]')))
print(price.get_attribute("value"))
driver.quit()
我正在做一些业余的网络抓取,但在通过 xpath 获取值时遇到了问题。我的目标是 https://kek.tools/t/0x4e15361fd6b4bb609fa63c81a2be19d873717870。我的代码如下所示:
def scrape_price():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
driver.get('https://kek.tools/t/0x841fad6eae12c286d1fd18d1d525dffa75c7effe')
driver.implicitly_wait(100)
price = driver.find_element_by_xpath('//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]')
print(price.get_attribute("value"))
driver.quit()
在检查元素中,//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]
过滤似乎得到了我需要的准确输入字段,但是当我打印值时总是返回“1”。 WebElement
我使用的方法不对吗?
而不是 implicitly_wait
尝试使用 expected_conditions
,如下所示:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def scrape_price():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
wait = WebDriverWait(driver, 20)
driver.get('https://kek.tools/t/0x841fad6eae12c286d1fd18d1d525dffa75c7effe')
price = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@class="sc-ciSkZP sc-bjHqKj bwSYJA dbQnLk" and @tabindex="2"]')))
print(price.get_attribute("value"))
driver.quit()