1

I can't seem to figure out how to return on integer from a table body. The terminal is returning a 'none' type and if I get rid of get.attribute("value") on line 10 nothing prints to the terminal.

from bs4 import BeautifulSoup as bs
import requests
from selenium import webdriver 

driver = webdriver.Safari()
url = driver.get('http://wsn.spaceflight.esa.int/iss/index_portal.php')

#able to get element from web but returning 'none' value in terminal
latitude = driver.find_element_by_xpath('//tbody/tr[1]/td[2]/div')

print(latitude.get_attribute("value"))#something I tried, not working
driver.close()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

From inspecting the webpage; you may need to try:

latitude = driver.find_element_by_xpath('//tbody/tr[1]/td[1]/div[2]')

Or this; since it has an ID:

latitude = driver.find_element_by_xpath('//*[@id="isst_lat"]')

And to get the content:

latitude.text

It is a string.

Nidhin Bose J.
  • 1,092
  • 15
  • 28
0

you can get the long and lat with below lines.

longitude = driver.find_element_by_xpath("//div[@id='isst_lon']").text
latitude = driver.find_element_by_xpath("//div[@id='isst_lat']").text

If you want to use the css then you can do it with

longitude = driver.find_element_by_css_selector('div#isst_lon').text
latitude = driver.find_element_by_xpath('div#isst_lat').text
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • I have used this method before and it doesn't output anything to the terminal. Do you think its being stored? Or is it just not return anything? –  Aug 28 '19 at 04:15
0

You can use locator by id to get latitude and longitude, like this:

driver = webdriver.Safari()
url = 'http://wsn.spaceflight.esa.int/iss/index_portal.php'
driver.get(url)

WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.ID, 'isst_lat')))

latitude = driver.find_element_by_id('isst_lat')
longitude = driver.find_element_by_id('isst_lon')

print(latitude.text)
print(longitude.text)

Following import:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

The above code I add WebDriverWait until visibility of element located.

frianH
  • 7,295
  • 6
  • 20
  • 45
0

To extract the value of Latitude you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.isst_cd#isst_lat"))).get_attribute("innerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Latitude']//following::div[1]"))).get_attribute("innerHTML"))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352