在尝试获取HTML内容时选择标签后。它正在转换值,例如0.102至0,102。有一些值,例如1,203,它们保持不变。但是浮点类型正在错误地转换。
我是新来的,所以我没有尝试过太多
element = driver.find_element_by_css_selector("div.widget-equity-technical-key-data table")
html = element.get_attribute('innerHTML')
说一个具有值1,201和0.102的列,预期至少1201和0.102,但实际输出为1,201和0,102
帖子findElement
方法您可以在字符串上使用替换方法,然后执行assert
值1,201
String s1 = s1.replace(',', '');
对于值0,102
String s1 = s1.replace(',', '.');
相关的 html 将有助于以更好的方式调试问题。但是,要使用Selenium检索 innerhtml ,您必须诱导 webdriverwait visibility_of_element_located()
,您可以使用以下任何一种解决方案:
-
使用
CSS_SELECTOR
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.widget-equity-technical-key-data table"))).get_attribute('innerHTML'))
-
使用
XPATH
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='widget-equity-technical-key-data']//table"))).get_attribute('innerHTML'))
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC