python - Send_keys to a textarea jsname in Python Selenium
问题描述
I'm trying to 'send_keys' to a textarea in Google Reviews using Python Selenium :
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.
解决方案
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&authuser=0&hl=en&pb=!2m1!1sChIJo2FtG0NpwokRn0ICTCCkHTw!3shttps%3A%2F%2Fwww.google.com!5sen!7b1&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.
推荐阅读
- css - Wordpress 网站标题在滚动页面元素后面移动
- java - 复制异步调用 - new Thread() - 更好的方法?
- go - 加油!RGB转灰度
- python - 合并两个数据帧 - 多索引 ValueError
- javascript - 在 Wordpress Admin 中插入简码
- javascript - Scaling a window and Component
- android - Android如何像调用应用程序一样在后台调用应用程序?
- javascript - AngularJS:在输入时将输入文本转换为标题大小写
- asp.net - ASP.NET 获取列表视图值并发布到文本框
- javascript - 如何使用正则表达式从 HTML 中清理 for-in-loop?