我正在尝试从使用xpath找到的表中获取一些数据。代码和输出如下(以下),我从各种来源的零碎代码中获取了此代码。我希望能够将值“ 16.06”打印为字符串,而不是“ [u'16.06']”,但我尝试对“ elems”变量使用拼接,但它似乎是FirefoxWebElement。因此,是否有将其转换为要拼接的字符串的方法,还是有其他解决方法?谢谢!

附件是网站的图片以及我要提取的内容。

enter image description here
用户名和密码已更改

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.common.exceptions import WebDriverException

driver = webdriver.Firefox()
url = "http://www.shareinvestor.com/user/login.html"


driver.get(url)
driver.implicitly_wait(10)

username = driver.find_element_by_name('name')
username.send_keys('XXX')

password = driver.find_element_by_name('password')
password.send_keys('XXX)

form = driver.find_element_by_id('sic_login_submit')
form.submit()

timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"

url = "http://www.shareinvestor.com/fundamental/financials.html?counter=D05.SI"
driver.get(url)

elems = driver.find_elements_by_xpath("""/html/body/div[2]/div[6]/div/div[1]/div/div[6]/table/tbody/tr[13]/td[2]""")
print([elm.text for elm in elems])


输出:
 -等待页面加载超时
 -[u'16.06']

最佳答案

(可能)这是以下内容的简单修复:

在elems变量的末尾,您可以添加.text来获取元素的文本。因此,只需在您的代码中执行:

elems = driver.find_elements_by_xpath("""/html/body/div[2]/div[6]/div/div[1]/div/div[6]/table/tbody/tr[13]/td[2]""").text


那应该工作正常。

它不返回您通常想要的内容的原因是因为您只是在尝试获取元素。您没有获取文本或其他任何文本,因此该类型与Web驱动程序所理解的相同,即FirefoxWebElement类型

这只是一个猜测,也许甚至不是您想要的,但这就是您所需要的。

关于python - FirefoxWebElement要字符串吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48707792/

10-09 00:15