我正在尝试在this页面上找到一个元素。具体而言,第一行的出价: 196.20p 。
我正在使用selenium,这是我的代码:
NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//*[@id=\"factsheet-tabs\"]/fund-tabs/div/div/fund-tab[3]/div/unit-details/div/div/unit-information/div/table[2]/tbody/tr[3]/td[2]'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"214","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62727","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"8faaff70-af12-11e7-a17c-416247c75eb6\", \"value\": \"//*[@id=\\\"factsheet-tabs\\\"]/fund-tabs/div/div/fund-tab[3]/div/unit-details/div/div/unit-information/div/table[2]/tbody/tr[3]/td[2]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8faaff70-af12-11e7-a17c-416247c75eb6/element"}}
Screenshot: available via screen
执行时,我收到以下错误
{{1}}
我在yahoo finance使用了相同的方法,但使用了不同的xpath,并且工作正常,但不幸的是我找不到那个价格。
答案 0 :(得分:1)
如果我没有理解你的要求那么这就是你想要的价格。我在这里使用了css选择器。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.trustnet.com/factsheets/o/g6ia/ishares-global-property-securities-equity-index-uk')
price = driver.find_element_by_css_selector('[ng-if^="$ctrl.priceInformation.Mid"] td:nth-child(2)').text
print(price.split(" ")[0])
driver.quit()
结果:
196.20p/196.60p
如果您想坚持使用xpath,请尝试以下方法:
price = driver.find_element_by_xpath('//*[contains(@ng-if,"$ctrl.priceInformation.Mid")]//td[2]').text