LdM
LdM

Reputation: 704

Access TextArea using selenium

I have a website www.example.com where I would like to extract some specific information. This website has a text area placeholder where to add some text. I am trying to use selenium as follows:

from selenium import webdriver

url = 'https://example.com/'

driver = webdriver.Chrome('my path/chromedriver')
driver.get(url)

The path where I can get information on Text Area should be

<body>
  <div class="index-wrap">==$0
...
  <div class="left-s-wrap">
   <div id="table">
    <div class="table">
     <div class="container">
      <div class="table__input"> ==$0
       ... 
       <div class="row tab">
        <div class="table__input-wrap table__input-wrap_multiline">
         <textarea placeholder="https://example.com" wrap="hard" spellcheck="false"></textarea>
          <button class="button button_accent-green">Add URLs</button>

Specifically the part which is going to be edited by entering urls is

         <textarea placeholder="https://example.com" wrap="hard" spellcheck="false">www.my_example.com</textarea>

I guess that I need to use find_element_by_xpath and click, similarly to this:

        textarea = site.find_element_by_xpath('//textarea')
        textarea.click()

But I am not familiar with process and I am a beginner in using these methods to access textarea. Any help on this will be extremely helpful!

Update question: Although I've followed the suggested method below, I cannot submit the query:

from selenium import webdriver
import time

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome('my path/chromedriver',chrome_options=chrome_options)
driver.get('https://www.example.com/')
textarea = driver.find_element_by_xpath('//textarea')
textarea.send_keys("www.my-example.com")

How can I click on submit to gather information from the website?

Upvotes: 0

Views: 1777

Answers (2)

Y sharifpour
Y sharifpour

Reputation: 389

You need to use the send_keys method to send a text to the textarea.

textarea = site.find_element_by_xpath('//textarea')
textarea.send_keys("www.example.com")

And you can use these codes to extract the value of the textarea

textarea = site.find_element_by_xpath('//textarea').get_attribute("value")

or

textarea = site.find_element_by_xpath('//textarea').text

UPDATE: to click on the "Add URLs" button, you can use this xpath:( With assuming that there are no other buttons with the same class before "Add URLs" button)

//button[@class='button button_accent-green']

if there are other buttons with the same class before the "Add URLs" button, you can use this xpath.

//button[text()='Add URLs']

So, the clicking code will be:

# with using the class name
button = site.find_element_by_xpath("//button[@class='button button_accent-green']")
# with using the text of the button
button = site.find_element_by_xpath("//button[text()='Add URLs']")
button.click()

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29382

Do you want to send something to that text area:

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//textarea[contains(@placeholder, 'https://example.com')]"))).send_keys('some text here ')

This is explicitWait, a way more reliable than find_element_by_xpath

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Read more here - official docs

Upvotes: 1

Related Questions