在我的代码中,我想从文本中获取数据值。我的代码是这样的:
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 = ["英镑", "欧元", "加元"]
我的输出不过=
为什么不起作用
我尝试过使用普通的 xpath,但这也不起作用。这是完整的 XPath
使用
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