首页 > 解决方案 > Send_keys to a textarea jsname in Python Selenium

问题描述

I'm trying to 'send_keys' to a textarea in Google Reviews using Python Selenium :

Textarea to send keys

This is the element:

<textarea jsname="B7I4Od" jsaction="focus:DTi7Pd; blur:PLbHqf; input:JD9Kbd; change:JD9Kbd; keyup:JD9Kbd; mouseup:nL5Qe" class="shklGc eFqywb T4CXLd" placeholder="Share your experience" aria-label="Enter review" id="Yc71gb" style="height: 90px;"></textarea>

I tried with:

        driver.find_element((By.XPATH, '//*[@id="Yc71gb"]')).send_keys('text')
        driver.find_element((By.XPATH, "//textarea[contains(@placeholder,'Share your experience')]")).send_keys('text')
        driver.find_element((By.XPATH, "//textarea[contains(@aria-label,'Enter review')]")).send_keys('text')
        driver.find_element((By.XPATH, "//textarea[contains(@jsname,'B7I4Od')]")).send_keys('text')
        driver.find_element((By.CSS_SELECTOR, '#Yc71gb')).send_keys('text')
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[aria-label='Enter review']"))).send_keys('text')

But none worked. Any idea?

Thank you.

标签: pythonselenium

解决方案


The text area is in iframe

<iframe name="goog-reviews-write-widget" role="presentation" class="goog-reviews-write-widget" src="https://www.google.com/maps/api/js/ReviewsService.LoadWriteWidget2?key=AIzaSyAQiTKe3tivKXammrJ6ov6u8E7KwZPNFss&amp;authuser=0&amp;hl=en&amp;pb=!2m1!1sChIJo2FtG0NpwokRn0ICTCCkHTw!3shttps%3A%2F%2Fwww.google.com!5sen!7b1&amp;cb=18503887"></iframe>

so you first have to switch to iframe then you can interact with the text area :

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@name='goog-reviews-write-widget']")))

You'll need imports as well :

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

after that WebDriverWait line you can write the line to identify text area and send the keys.


推荐阅读