无法使用selenium从元素中获取数据值

问题描述 投票:0回答:1

在我的代码中,我想从文本中获取数据值。我的代码是这样的:

    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="chromedriver.exe")
    driver.get('https://www.google.nl')
    accept = driver.find_element("xpath",'//*[@id="L2AGLb"]/div')
    accept.click()
    box =     driver.find_element("xpath",'/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
    box.send_keys("placeholder")
    box.send_keys(Keys.ENTER)
    for x in AmountInputFinal:
        close = driver.find_element("xpath", '/html/body/div[4]/div[2]/form/div[1]/div[1]/div[2]/div/div[3]/div[1]/div')
        close.click()
        search = driver.find_element("xpath",'/html/body/div[4]/div[2]/form/div[1]/div[1]/div[2]/div/div[2]/input')
        search.click()
        search.send_keys(f"{AmountInput} Dollar to {x}")
        search.send_keys(Keys.ENTER)
        try:
            Exchange = driver.find_element("xpath", f'/html/body/div[7]/div/div[11]/div/div[2]/div[2]/div/div/div[1]/div/div/div/div/div/div/div/div[3]/div[1]/div[1]/div[2]/span[1]').get_attribute('data-value')
        except:
            Exchange = driver.find_element("xpath", f'/html/body/div[7]/div/div[10]/div/div[2]/div[2]/div/div/div[1]/div/div/div/div/div/div/div/div[3]/div[1]/div[1]/div[2]/span[1]').get_attribute('data-value')
        Exchange = len(Exchange)

在本例中 {AmountInput} = "Ten" 且 AmountInputFinal = ["英镑", "欧元", "加元"]

我的输出不过=

  • 12
  • 6
  • 18

为什么不起作用

我尝试过使用普通的 xpath,但这也不起作用。这是完整的 XPath

python selenium xpath webdriver webdriverwait
1个回答
0
投票

使用

WebDriverWait()
并等待
visibility_of_element_located()
并执行以下
xpath
选项

Exchange=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[@data-value]"))).get_attribute("data-value")
print(Exchange)

您需要导入以下库:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
© www.soinside.com 2019 - 2024. All rights reserved.