我正试图做一些对我真正有用的事情,但我遇到了一个问题。
这是给我结果的代码行";5.0公里";
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from time import sleep
#C:UsersjeffersonAppDataRoamingPythonPython39site-packageswebdriver_managerdrivers
servico = Service(ChromeDriverManager().install())
navegador = webdriver.Chrome(service=servico)
navegador.get("https://www.google.com.br/maps/dir//")
#digitar primeiro endereço
navegador.find_element('xpath', '//*[@id="sb_ifc50"]/input').send_keys("Rimatur Transportes, Rodovia Curitiba - Ponta Grossa Br-277, Km 2,1875 - Mossunguê, Curitiba - PR, 82305-100")
navegador.find_element('xpath', '//*[@id="sb_ifc50"]/input').send_keys(Keys.ENTER)
#destino
navegador.find_element('xpath', '//*[@id="sb_ifc51"]/input').send_keys("Av. Juscelino Kubitschek De Oliveira - Ld, 2600 - Cidade Industrial De Curitiba, Curitiba - PR, 81260-900")
navegador.find_element('xpath', '//*[@id="sb_ifc51"]/input').send_keys(Keys.ENTER)
#def directions():
# sleep(10)
# directions = navegador.find_element_by_xpath("/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[4]/div[1]/button")
# directions.click()
#directions()
def km():
sleep(10)
kmtotal=navegador.find_element('xpath', '/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div')
txt = kmtotal.text
value = float(txt)
print(value)
km()
我只想打印";5,0〃;如果没有KM来装载路由,我如何修改代码以将其打印/保存在变量中?
谢谢<3
很多事情:(
仅打印数字和逗号
这应该有效:
def km():
sleep(10)
kmtotal=navegador.find_element('xpath', '/html/body/div[3]/div[9]/div[9]/div/div/div[1]/div[2]/div/div[1]/div/div/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div')
txt = kmtotal.text #extract the entire text content from the web element
value = float(txt) # extract the float from the string
print(value) # print it
km()