繁体   English   中英

如何获得价值 <input> 在硒python中动态加载的占位符?

[英]How to get the value in <input> placeholder which loads dynamically in selenium python?

我正在尝试抓取一个网站,您需要在该网站上传递地址以获得该地址的坐标。 我能够传递地址并在浏览器中显示纬度和经度,但无法检索它。

这是怎么回事。

使用此代码,我在浏览器中显示了纬度和经度。

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()

现在显示纬度和经度:

在此处输入图片说明

如何获取在输入字段中动态生成的纬度和经度,因此无法在inspect元素中找到。

经度和纬度的检查元素是

纬度

<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值的函数,我们可以使用硒的execute_script吗

您可以使用输入节点的'value'属性获取值,请尝试以下代码:

from selenium import webdriver
from time import sleep

wd = webdriver.Chrome('C:\NotBackedUp\chromedriver.exe')
wd.get('https://www.latlong.net/convert-address-to-lat-long.html')
inputElement = wd.find_element_by_xpath('//input[@placeholder="Type address here to get lat long"]')
inputElement.send_keys('Domlur, Bangalore')
wd.find_element_by_id('btnfind').click()

sleep(8)
print(wd.find_element_by_id('lat').get_attribute('value'))
print(wd.find_element_by_id('lng').get_attribute('value'))

您需要做的就是,在获取值之前需要稍加延迟。 我使用过sleep()方法,但不建议使用该方法,您也可以尝试其他一些等待。 希望对您有帮助...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM