1

I'm using Selenium in a Python script to query NASA's Small Body Database for the location of asteroids. I've been working with a Python shell to check that my commands are getting the results I want, but I'm having a consistent issue with the "submit" method.

The page I'm working on is https://ssd.jpl.nasa.gov/sbwobs.cgi. All I want to do is have Selenium click on the "change" link next to "Observation Time," fill in a field on the page that opens, and then click the resulting "Use Specified Time" button.

I've set up a Selenium WebDriver object named "browser", and then the first command I send is:

browser.find_element_by_css_selector("a[href*='time']").click()

Which works and sends me to my target page, which is https://ssd.jpl.nasa.gov/sbwobs.cgi?s_time=1#top

I then find the text field and fill it in with:

browser.find_element_by_name('obs_time').send_keys("2021-04-01 01:00")

... last thing I need is to click the "Use Specified Time" button to accept this. I tried with:

browser.find_element_by_name("check_time").click()

.. but this had no response. Then I tried:

browser.find_element_by_name("check_time").submit()

... which seems to work, but takes me back to the previous page without actually changing the time. If I manually click on the button it would work fine.

Selenium is definitely finding the right element, and the submit method is doing something, but it's maybe not sending the info in the text box correctly.

I don't know anywhere near enough about HTML etc to be able to figure this out. Can anyone offer any advice?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
MrFox
  • 21
  • 2

2 Answers2

2

You were close enough. Before you set the new Observation Time as 2021-04-01 01:00 you need to clear up the previous input. Ideally, to change the Observation Time you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://ssd.jpl.nasa.gov/sbwobs.cgi')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='time']"))).click()
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='obs_time']")))
    element.click()
    element.clear()
    element.send_keys("2021-04-01 01:00")
    driver.find_element_by_css_selector("input[name='check_time']").click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'time')]"))).click()
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='obs_time']")))
    element.click()
    element.clear()
    element.send_keys("2021-04-01 02:00")
    driver.find_element_by_xpath("//input[@name='check_time']").click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

NASA

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Unfortunately that gives me the same result; using the `wait` command did help make it flow as a script without issues, but that final click command just results in silence from the webpage. `driver.find_element_by_css_selector("input[name='check_time']").click()` ... does seem to work, in that Selenium doesn't give me any errors and doesn't say it can't find the element. I'm beginning to think trying with Chrome or Firefox before looking further might be a good option! – MrFox Jun 24 '20 at 11:45
0

Thanks to @DebanjanB for the comprehensive code that improved all my methods; however, I found that everything worked as expected with his code and mine by switching to Firefox.

Evidently it's some anomaly in Safari's web driver that was causing the issue.

MrFox
  • 21
  • 2