2

I want to copy all values from textbox using selenium. so far my code looks like

# -*- coding: UTF-8 -*
from selenium import webdriver    #open webdriver for specific browser
import requests
import time

def getListZip(zip,radius):
    browser = webdriver.Chrome()
    browser.get("https://www.freemaptools.com/find-zip-codes-inside-radius.htm")
    time.sleep(10)
    user = browser.find_element_by_css_selector("#tb_radiuskm")
    user.clear()
    user.send_keys(radius)
    user = browser.find_element_by_css_selector("#goto")
    user.clear()
    user.send_keys(zip)
    time.sleep(10)
    drawRadius = browser.find_element_by_css_selector("#contenttext > center:nth-child(8) > input:nth-child(1)")
    drawRadius.click()
    time.sleep(10)
    listZip= browser.find_element_by_xpath('//*[@id="tb_output"]').text
    return listZip

def main():
    zip = getListZip(43212,25)

if __name__ == "__main__":
    main()    

this should return almost 70 values but it returns null. This program will give input as zip code and radius and will get output of all zipcode within specific radius.

I am using python 3.x

2 Answers 2

5

The element you are targeting is a TEXTAREA. .text gets the text between the open and close tags of an element, e.g. <div>.text gets this text<div>. The TEXTAREA element holds it's text inside the value attribute. You can get this using

listZip = browser.find_element_by_css_selector("#tb_output").get_attribute("value")

I changed the locator here because you didn't need XPath. CSS selector or by ID is faster.

Bonus:

You can clean up your selector for drawRadius by using the below. It's more specific than nth-child, etc.

drawRadius = browser.find_element_by_css_selector("input[value='Draw Radius']")
Sign up to request clarification or add additional context in comments.

Comments

0

I have tested Selenium on Java, and frequently I had the same problem. The element with id "tb_output" contains other elements? Try using...

listZip= browser.find_element_by_xpath('//*[@id="tb_output"]').innerHTML

instead:

listZip= browser.find_element_by_xpath('//*[@id="tb_output"]').text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.