【发布时间】:2019-06-26 05:47:09
【问题描述】:
我试图抓取一个网站,您需要在其中传递地址以获取该地址的坐标。我能够传递地址并在浏览器中显示经纬度,但无法检索。
事情是这样的。
使用此代码,我得到了在浏览器中显示的纬度和经度。
from selenium import webdriver
chrome_path = r"C:\Users\Himanshu Poddar\Desktop\chromedriver.exe"
url = 'https://www.latlong.net/convert-address-to-lat-long.html'
wd = webdriver.Chrome(chrome_path)
wd.get(url)
# Get the input element to which address is to be passed
inputElement = wd.find_element_by_xpath('//input[@placeholder="Type address here to get lat long"]')
# send the address
inputElement.send_keys('Domlur, Bangalore')
# click on get the coordinates button
wd.find_element_by_id('btnfind').click()
现在显示纬度和经度:
如何获取在输入字段中动态生成的纬度和经度,因此在检查元素中找不到。
经纬度的检查元素是
纬度
<div class="col-6 m2">
<label for="lat">Latitude</label>
<input type="text" name="lat" id="lat" placeholder="lat coordinate">
</div>
经度
<div class="col-6 m2">
<label for="lng">Longitude</label>
<input type="text" name="lng" id="lng" placeholder="long coordinate">
</div>
其中不包含显示的经纬度。
编辑:我正在寻找返回 latlong 值的函数,我们可以使用 selenium 的 execute_script 来完成吗
【问题讨论】:
-
@JeffC 虽然其中一个解决方案告诉我们可以这样做,但我的问题是专门询问我们如何使用其他方法
-
还有什么方法?你的问题不清楚。您没有尝试使用
.getAttribute("value")和结果的代码。链接的问题向您展示了如何执行此操作。使用该代码,它将起作用。如果没有,请返回并使用您使用的确切代码和结果编辑您的问题。 -
@JeffC 我可以向您展示很多询问“如何做”的问题,即使 OP 没有显示实现解决方案的方法。我只想问一件事告诉我如果我不知道那你还指望我怎么尝试?我的意思是我没有尝试过我搜索了关于 SO 的所有相关问题并尝试了所有这些。如果它不起作用,那么我该如何发布甚至不相关的错误方法。
-
如果您不知道如何,您可以搜索并阅读。你找到一些可能有用的东西并尝试它们。在 SO 上不好的一件事是在没有尝试和解释的情况下提出问题。您也许可以链接执行此操作的帖子,但它们可能已有数年之久......即使它们不是,这也不是重复错误行为的借口。
标签: python selenium-webdriver web-scraping