【发布时间】:2020-12-08 05:10:16
【问题描述】:
我对此很陌生,我正在挑战自己的第一个有用项目,超越井字游戏和基于文本的选择你自己的冒险游戏。我正在尝试编写一个脚本,该脚本将显示某些房屋的基本价格何时超过其当前值,然后向我发送一条短信说它已经上涨。现在,我只需要帮助获取使用 BS4 或 Selenium 在 pycharm 中打印的价格。到目前为止,我已经让 selenium 找到并打开页面。当我检查页面上的 Base Price 元素时,它返回为:
> "<h3 ng-show="model.mod_basePrice" class="ng-binding"> Base Price
> $278,900</h3>"
。我已经尝试了所有可以找到的方法来显示标题或价格。我整天都在这。我错过了一些东西,我无处可去。
import selenium
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.legacyhomesal.com/pennington-freedom-series-richmond-ii")
price = driver.find_elements_by_class_name(name='ng-binding')
print(price)
我已经尝试了这些以及更多:
driver.find_elements_by_class_name(name='ng-binding')
driver.find_elements_by_class_name('ng-binding')
driver.find_element_by_class_name(name='ng-binding')
driver.find_element_by_class_name('ng-binding')
driver.find_element_by_xpath("//*[@id='page']/section[2]/main/div[2]/div/div[2]/aside/h3")
我也创建了自己的 Xpath。当我检查页面上的基本价格元素并找到 //h3[@class='ng-binding' 它确实突出显示了正确的元素,所以至少我知道 xpath 有效。我也尝试了以下没有结果:
price = driver.find_element_by_xpath(xpath="//h3[@class='ng-binding']")
print(price)
我得到的结果是:
C:\Users\kgood\AppData\Local\Microsoft\WindowsApps\python.exe "C:/Users/kgood/Desktop/Legacy R2 Price.py"
<selenium.webdriver.remote.webelement.WebElement (session="0c6f3462267a3648682858dc0fd0d3c4", element="fdddf0be-c2ca-4466-996f-caac9d31e269")>
Process finished with exit code 0
【问题讨论】:
-
试试 price.text。应该输出 Base Price $278,900。
标签: python selenium xpath css-selectors webdriverwait