Bhavesh Ghodasara
Bhavesh Ghodasara

Reputation: 2071

copy text area using selenium python

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

Upvotes: 2

Views: 2666

Answers (2)

JeffC
JeffC

Reputation: 25596

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

Upvotes: 5

JordiSilv
JordiSilv

Reputation: 36

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

Upvotes: 0

Related Questions