0
我試圖在this的頁面上找到元素。特別是第一行中的競標價格:196.20p。Selenium - 找不到xpath的元素
我使用的硒,這是我的代碼:
from selenium import webdriver
driver = webdriver.PhantomJS()
address = 'https://www.trustnet.com/factsheets/o/g6ia/ishares-global-property-securities-equity-index-uk'
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]'
price = driver.find_element_by_xpath(asset['xpath'])
print price.text
driver.close()
執行時我收到以下錯誤
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
我用同樣的方法,但使用不同的XPath,在yahoo finance和它工作正常,但不幸的是,我正在尋找的價格在那裏不可用。
我已經接受了答案,因爲它解決了原來的問題。一些評論雖然。 1.它似乎比xpath慢得多;這是正常的嗎? 2.它不適用於PhantomJS。謝謝 – Rojj
如果出現與速度有關的問題,它會有所不同。很少有人說css選擇器比較好,因爲它易於閱讀和理解,而且很少推薦xpath,因爲它能夠以您期望的方式遍歷Dom。但是,要明確您的想法,您可能需要查看http://elementalselenium.com/tips/32-xpath-vs-css此鏈接。 – SIM