gis.rajan
gis.rajan

Reputation: 517

Extracting div tags without any class and id using selenium and python

I am trying to get Hourly and Daily water level data from website using selenium.

my code so far

import time 
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(r"C:\Python27\chromedriver.exe")
driver.get('http://hydrology.gov.np/#/basin/77?_k=1r1onx')
time.sleep(5)
driver.find_elements_by_xpath("//*[contains(text(), 'Hourly')]").click()

But its not working. I am new to selenium. I have also tried other methods to find elements but not succedded. I will appreciate any help in this regard.

code from the html

  div style="padding: 16px 0px; display: table-cell; user-select: none; width: 96px;">
    <div>
        <span tabindex="0" style="border: 10px; box-sizing: border-box; display: block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: 15px; font-weight: inherit; position: relative; color: rgba(0, 0, 0, 0.87); line-height: 32px; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; min-height: 32px; white-space: nowrap; background: none;">
            <div>
                <div style="margin-left: 0px; padding: 0px 24px; position: relative;">
                    <div>Point</div>
                </div>
            </div>
        </span>
    </div>
    <div>
        <span tabindex="0" style="border: 10px; box-sizing: border-box; display: block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: 15px; font-weight: inherit; position: relative; color: rgb(255, 64, 129); line-height: 32px; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; min-height: 32px; white-space: nowrap; background: none;">
            <div>
                <div style="margin-left: 0px; padding: 0px 24px; position: relative;">
                    <div>Hourly</div>
                </div>
            </div>
        </span>
    </div>
    <div>
        <span tabindex="0" style="border: 10px; box-sizing: border-box; display: block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: 15px; font-weight: inherit; position: relative; color: rgba(0, 0, 0, 0.87); line-height: 32px; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; min-height: 32px; white-space: nowrap; background: none;">
            <div>
                <div style="margin-left: 0px; padding: 0px 24px; position: relative;">
                    <div>Daily</div>
                </div>
            </div>
        </span>
    </div>
</div>

Upvotes: 0

Views: 1240

Answers (2)

Ishita Shah
Ishita Shah

Reputation: 4035

//div[contains(text(),'Hourly')] Your xpath seems correct as according to HTML, Please check with Debugger and you will be trace where the problem is,

You can click element by 3 ways:

  1. driver.click()
  2. Action Click

    from selenium.webdriver.common.action_chains import ActionChains
    targetElement = driver.find_element_by_xpath("//div[contains(text(),'Hourly')]")
    actions = ActionChains(driver)
    actions.move_to_element(targetElement).click().perform()

  3. javascript executor click

    driver.execute_script("arguments[0].click();", element)

Upvotes: 1

demouser123
demouser123

Reputation: 4264

This

driver.find_elements_by_xpath("//*[contains(text(), 'Hourly')]").click()

using find_elements will return a list of elements. You'll have to iterate through the list to click on them.

To get the hourly data, you'll need to replace this line with

table_body = driver.find_element_by_css_selector('table.table.tab-bar > tbody')
print(table_body.text)

which will give data like

Jun 18, 2018 6:00 AM 1.94 -0.15 0.89
Mon, Jun 18, 2018 7:00 AM 1.93 1.92 1.93
Mon, Jun 18, 2018 8:00 AM 1.91 1.91 1.91
Mon, Jun 18, 2018 9:00 AM 1.9 1.89 1.89
Mon, Jun 18, 2018 10:00 AM 1.88 1.88 1.88
Mon, Jun 18, 2018 11:00 AM 1.87 1.87 1.87
Mon, Jun 18, 2018 12:00 PM 1.88 1.88 1.88
Mon, Jun 18, 2018 1:00 PM 1.88 1.87 1.88
Mon, Jun 18, 2018 2:00 PM 1.84 1.84 1.84
Mon, Jun 18, 2018 3:00 PM 1.86 1.84 1.85
Mon, Jun 18, 2018 4:00 PM 1.77 1.77 1.77
Mon, Jun 18, 2018 5:00 PM 1.68 1.68 1.68
Mon, Jun 18, 2018 6:00 PM 1.63 1.62 1.62
Mon, Jun 18, 2018 7:00 PM 1.62 1.62 1.62

Upvotes: 1

Related Questions