Send_keys to a textarea jsname in Python Selenium - python

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.

Related

How can I download a file using Selenium?

I want to download a xlsx file from a website. And to do that I have to click on a button and I can't find it with my Python code. With my current Python code I can open the website using Selenium and open the download page and what I want is to click on "Excel: extension XLSx" button.
options=webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver=webdriver.Chrome(executable_path=path_web_crawler+"chromedriver.exe",chrome_options=options)
driver.get("https://www.ine.es/jaxiT3/Datos.htm?t=25171")
time.sleep(3)
driver.find_element_by_xpath("//input[#name='btnDescargaForm']").click()
What I tried to detect the button
elem=driver.find_element_by_xpath("//input[#value='xlsx']")
driver.execute_script("arguments[0].click();",elem)
<iframe title="Popup content - Rising content" id="thickBoxINEfrm" name="thickBoxINEfrm" frameborder="0" width="100%" height="100%" marginheight="0" marginwidth="0" tabindex="0" onload=";autoResizeThickBox(this)"></iframe>
Your element is in an iframe.
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Full Code:
driver.get("https://www.ine.es/jaxiT3/Datos.htm?t=25171")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[#name='btnDescargaForm']"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"thickBoxINEfrm")))
elem=wait.until(EC.presence_of_element_located((By.XPATH,"//input[#value='xlsx']")))
driver.execute_script("arguments[0].click();",elem)

Difficult button to press in selenium

how to press the "f_agreements_all" button ?
<label for="f_agreements_all">
<input type="checkbox" id="f_agreements_all">
<span></span>
<span class="permText">I accept all</span>
</label>
Unfortunately, finding "f_agreements_all" button and clicking it doesnt work. Span covers the whole button17x17px, when f_agreements_all is 16x16 under it. Do you know possible way to click it?
Using vanilla JS one way to do it would be:
const checkbox = document.getElementById('f_agreements_all');
checkbox.click();
Quickly looking at the Python Selenium 2 WebDriver API docs, one can try:
driver.find_element_by_id("f_agreements_all").click()
Can you please try below xpath :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button=WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//li[#class='formCheckbox agreements all-agreements']//span[1]")))
button.click()
To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='f_agreements_all']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='f_agreements_all']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium(with python): How to enter any text into wiki textarea

I have one question on Selenium which is how can I enter text into textarea (wiki textarea)? Below is my original HTML, please help me to figure out this. Thank you very much!
<textarea class="textarea long-field wiki-textfield mentionable wiki-editor-initialised wiki-edit-wrapped" cols="60" id="comment" name="comment" wrap="virtual" data-projectkey="PE15" data-issuekey="PE15-2181" resolved="" style="min-height: 174px; max-height: 369px;"></textarea>
text_area = driver.find_element_by_id('comment')
text_area.send_keys("This text is send using Python code.")
As per the HTML you have provided to send character sequence into the text area you need to induce WebDriverWait as follows:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.textarea.long-field.wiki-textfield.mentionable.wiki-editor-initialised.wiki-edit-wrapped#comment"))).send_keys("Ben_C")
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[#class='textarea long-field wiki-textfield mentionable wiki-editor-initialised wiki-edit-wrapped' and #id='comment']"))).send_keys("Ben_C")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

click on anchor tag having href = '#'

I am using below python code using selenium. click is not working on anchor tag having href = "#"
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("E:\chromedriver.exe")
driver.get('file:///E:/Selenium/validateTest.html')
driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']").click()
Here is the web html code that I am using.
<h1>Anchor tag</h1>
Show content
<i class="fa fa-binoculars" aria-hidden="true"></i> Validate Data
As per the HTML you have shared it seems the AUT is based on JavaScript, so to click on the link with text as Validate Data you have to induce WebDriverWait for the element to be clickable and you can use either of the following options :
LINK_TEXT :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Validate Data"))).click()
CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-red#validateData"))).click()
XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-red' and #id='validateData']"))).click()
Note : You will require the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try using a javascript executor to click your element.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement elementToClick = driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']");
js.executeScript("arguments[0].click();", elementToClick);
Above code needs to be adapted to python ( which i'm not familiar with, but you get the idea)
Hope this helps

I can not find a way to click on a button using Python and Selenium

I have a problem, I happen to have a button with these labels, I'm trying to click through Selenium but I can not find a way to click it. I tried to give it taking as reference the XPath, link text, and CSS selector but I do not achieve my goal. This is the code for the button:
<a class="btn btn-flat pull-right" data-action="export_report"> <i class = "icon-export"> </ i> Export </a>
East of the XPath:
// * [# id = "reports"] / div [1] / div [2] / a
this selector:
#reports> div.span12> div.headline-action-block.pull-right> a
This is the button and my code in Python :(
Button:
My code:
I face this error:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
'value': value})['value']
You can try this:
openrate = driver.find_element_by_css_selector("a.btn")
openrate.click()
Assuming this is the only button, or the first button on the page. Otherwise ("a.btn.btn-flat.pull-right")
As per the HTML you have shared it seems you have invoked click() in the previous step so in this step to click() on the button with text as Export you have to induce WebDriverwait for the element to be clickable and you can use either of the following Locator Strategies :
PARTIAL_LINK_TEXT :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export"))).click()
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-flat.pull-right[data-action='export_report']>i.icon-export"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-flat pull-right' and #data-action='export_report']/i[#class='icon-export']"))).click()

Categories

Resources