我希望用print((命令输出页面中的值,但遗憾的是,没有输出任何值,也没有显示任何错误。
website:https://coordvert.com/en/koordinaten-umrechnen/utm/dezimalgrad?utmzone=-1N&utme=668184.607&utmn=0.000
这是代码:
breitengrad=driver.find_element_by_xpath('(//div[@class="input group input group sm"]([4]/input'(.text
打印(breitengrad(
您要查找的元素(breitengrad(似乎有一个ID,因此只通过ID定位它更健壮。此外,如果您使用的是Selenium 4,则find_element_*
方法已被弃用,取而代之的是find_element(By.*,...)
方法。如果出现初始弹出窗口,以下代码将接受cookie,等待页面加载,定位breitengrad
并打印出值:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url='https://coordvert.com/en/koordinaten-umrechnen/utm/dezimalgrad?utmzone=-1N&utme=668184.607&utmn=0.000'
browser.get(url)
try:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Alles Akzeptieren']"))).click()
except Exception as e:
print('no pop-up, moving on')
breitengrad = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "cov-2-lat")))
print(breitengrad.get_attribute('value'))
这将返回该输入字段的值:
0