簡體   English   中英

使用 Selenium (Python) 從輸出框中獲取值

[英]Get Value from Output Box using Selenium (Python)

我正在嘗試根據我在另一個文本框中輸入的值提取在文本框中生成的文本。 我查看了檢查元素,根本沒有值的跡象,即使填充了框,“值”中也沒有任何內容。

我在 Python 中使用 selenium 來嘗試執行此操作。 我目前只使用 1 個,但將設置一個循環來執行數千個操作,因此我需要這個自動化。

以下是頁面中的代碼(愛爾蘭條例調查網站

 <td width="25%" class="form">Latitude:</td> <td class="form"> <input type="text" name="GeodeticLatitude" type="number" size="10" maxlength="2" value=""> deg <input type="text" name="GeodeticLatitudeMin" size="2" maxlength="2" value="0"> min <input type="text" name="GeodeticLatitudeSec" size="8" maxlength="8" value="0"> sec </td>

以下是我目前正在嘗試提取值的代碼

browser = webdriver.Chrome()

browser.get("https://gnss.osi.ie/new-converter/")

def find():
    python_button = browser.find_elements_by_xpath("//input[@name='IrishGridEasting']")[0]
    python_button.send_keys("316600")
    python_button = browser.find_elements_by_xpath("//input[@name='IrishGridNorthing']")[0]
    python_button.send_keys("229500")
    python_button = browser.find_elements_by_xpath("//td[@class='form']/button[@type='button']")[1]
    python_button.click()

    latDeg = browser.find_elements_by_xpath("//input[@name='GeodeticLatitude']")
    print(latDeg)

我試圖添加諸如 .text、.getText()、.getAttribute 和 .get_attribute 等選項,但它們不返回文本框值

然后下面的屏幕截圖顯示了我試圖檢索的內容。

紅色框是我要插入的數字,綠色框代表我要提取的內容。

網頁截圖

您必須提供time.sleep(1)因為生成腳本的值無法同步 this.Try WebDriverWait並使用get_attribute('value')從輸入字段中獲取值。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver=webdriver.Chrome()
driver.get('https://gnss.osi.ie/new-converter/')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@name='IrishGridEasting']"))).send_keys("316600")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@name='IrishGridNorthing']"))).send_keys("229500")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//tr[contains(.,'Irish Grid Co-ordinates:')]//button[text()='Convert']"))).click()
time.sleep(1)
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"input[name='GeodeticLatitude'][value]"))).get_attribute('value'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM