简体   繁体   中英

Get Value from Output Box using Selenium (Python)

I am trying to extract the text that is generated in a text box based on values I enter into another text box. I looked in the inspect element and there is no signs of the values at all, and the 'Value' has nothing in it even though the box is populated.

I am using selenium in Python to try perform this. I am only working with 1 currently, but will be setting up a loop to do thousands hence why I need this automated.

Below is the code in the page ( Ordinance Survey Ireland Website )

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

Below is the code that I am currently working with to attempt to extract the values

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)

I have attempted to add in options such as .text, .getText(), .getAttribute and .get_attribute but they do not return the text box value

Then below screenshot shows what I am trying to retrieve.

The red boxes is the number I am inserting, the green box represents what I want to extract.

网页截图

You have to provide time.sleep(1) since the value generating your script unable to sync this.Try WebDriverWait and use get_attribute('value') to get the value from input fields.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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