Selenium Webdriver to insert text from python in text area - python

<textarea style="font-size: 1.2em" ng-model="applications" ng-list=" " ng-trim="false" rows="15" cols="70" class="ng-valid ng-dirty ng-valid-parse ng-touched"></textarea>
This is the text area tag. I want to insert some text into the text area. How can I proceed with the python code for the selenium webdriver.
I tried:
driver.find_element_by_class_name("ng-pristine ng-valid ng-touched")
Please help me out.

To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("textarea.ng-valid.ng-dirty.ng-valid-parse.ng-touched[ng-model='applications']").send_keys("samhith gardas")
Using xpath:
driver.find_element_by_xpath("//textarea[#class='ng-valid ng-dirty ng-valid-parse ng-touched' and #ng-model='applications']").send_keys("samhith gardas")
However the desired element is a Angular element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.ng-valid.ng-dirty.ng-valid-parse.ng-touched[ng-model='applications']"))).send_keys("samhith gardas")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[#class='ng-valid ng-dirty ng-valid-parse ng-touched' and #ng-model='applications']"))).send_keys("samhith gardas")
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

driver.find_element_by_class_name("ng-pristine.ng-valid.ng-touched").
Or
driver.find_element_by_xpath("//textarea[#claaa='ng-pristine ng-valid ng-touched']")
Class with space indicates multiple classes , so you have to replace space with dot in find_element_by_class
Else use xpath and find the element that matches absolute attribute value of the class

Related

Selenium send_keys doesn't send uppercase R within the input bar

I'm using selenium 4.1.0 and I'm trying to send 'CRISTIAN' in an input bar through
input_bar.send_keys('CRISTIAN')
But it shows 'CISTIAN' in the bar.
I've tried also tried:
ActionChains(driver).click(input_bar).send_keys('CRISTIAN', Keys.ENTER).perform()`
But I get the same result. I checked all the uppercase letters and I figured out that only R have this problem. Any suggestions? Does it depends on this version of Selenium?
HTML of the input bar:
<div _ngcontent-qhp-c128="" cdkdroplist="" cdkdroplistorientation="horizontal" cdkdroplistdisabled="" class="cdk-drop-list d-flex flex-1 cdk-drop-list-disabled" id="cdk-drop-list-16"><input _ngcontent-qhp-c128="" cdkdrag="" data-bp="input" class="cdk-drag comp-input mt-2 cdk-drag-disabled" placeholder="Aggiungi elemento"><!----><!----><!----><!----></div>
The desired element is a Angular element, to send a character sequence you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id^='cdk-drop-list'][cdkdroplistorientation='horizontal'] input[class*='cdk-drag'][data-bp='input'][placeholder='Aggiungi elemento']"))).send_keys('CRISTIAN')
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#cdkdroplistorientation='horizontal' and starts-with(#id, 'cdk-drop-list')]//input[contains(#class, 'cdk-drag') and #data-bp='input'][#placeholder='Aggiungi elemento']"))).send_keys('CRISTIAN')
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

Trying to make selenium click on a button

I am trying to make selenium click the add to trolley button but there is an error the code which I am using is:
trolley = driver.find_element_by_xpath("//button[#role='button']")
trolley.click()
The inspect element of the button is:
<button class="Buttonstyles__Button-pv6mx8-2 SczzF" data-test="add-to-trolley-button-button" kind="primary" role="button" tabindex="0" type="button" xpath="1" style=""><span><span>Add<span class="sr-only"> </span> to trolley</span></span></button>
I was able to find a pythonspot article on clicking a button using selenium (https://pythonspot.com/selenium-click-button/) and they used the function:
trolley = driver.find_elements_by_xpath(`xpath here`)
trolley.click()
The difference between them is this one returns a list over a single element, im not sure why this might work over your method but this is what ive found. Just change that element to elements
To locate the element you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.find_element(By.CSS_SELECTOR, "button[class^='Buttonstyles__Button'][data-test='add-to-trolley-button-button'] > span > span").click()
Using XPATH:
driver.find_element(By.XPATH, "//button[starts-with(#class, 'Buttonstyles__Button') and #data-test='add-to-trolley-button-button']/span/span").click()
Ideally, to locate the clickable element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class^='Buttonstyles__Button'][data-test='add-to-trolley-button-button'] > span > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Buttonstyles__Button') and #data-test='add-to-trolley-button-button']/span/span"))).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

Python Selenium - Any way to get send text within this textbox?

I'm tring to select a textbox to input some data into it, the problem is i don't know how to get it's element:
This is what i get whem i do inspect on it:
<input class=" input" maxlength="255" type="text" aria-describedby="" placeholder="" id="8624:0" data-aura-rendered-by="8628:0" data-interactive-lib-uid="28">
The thing is that this is basically to fill forms, and id, rendered by, and uid numbers change.
Is there anything i could try?
To send a character sequence to the <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[type='text'][data-aura-rendered-by][aria-describedby]"))).send_keys("Smece")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class=' input' and #type='text'][#data-aura-rendered-by and #aria-describedby]"))).send_keys("Smece")
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

How to get this element and click on it with selenium?

Trying to make my bot click on a submit button.
<div class="usertext-buttons">
<button type="submit" onclick="" class="save">save</button>
<button type="button" onclick="return cancel_usertext(this);" class="cancel" style="display:none">cancel</button>
<span class="status"></span></div>
I want to get the second row element with the type="submit"
driver.find_element_by_xpath doesn't work since the xpath is different for every post. What can I pull here that generally works?
To click on the element with text as save you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.save[type='submit'][onclick]").click()
Using xpath:
driver.find_element_by_xpath("//button[#class='save' and text()='save'][#type='submit' and #onclick]").click()
Ideally, to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.save[type='submit'][onclick]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='save' and text()='save'][#type='submit' and #onclick]"))).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
Try using css selector:
driver.find_element_by_css_selector('div.usertext-buttons > button[type=submit]').click()

How to wait for an element and click using name attribute

I have the below html mark-up I am trying to access and click via python... for some reason copying the xpath and doing this is not working:
self.driver.find_element(By.XPATH, '//*`[#id="isc_8D"]/table/tbody/tr/td/table/tbody/tr/td[2]/img')`
It seems the 'name' attribute is the only unique identifier below; how could I WAIT for it to exist first, then find element by name attribute and click in python? i.e. name="isc_NXicon"
<img src="http://website:8080/DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png" width="12" height="12" align="absmiddle" style="vertical-align:middle" name="isc_NXicon" eventpart="icon" border="0" suppress="TRUE" draggable="true">
I am doing the below via different element with CSS selector: But How could I do the same via html 'name attribute' for my current relevant mark-up?
WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn.btn-mini.btn-primary"))).click()
To locate and click() on the desired element instead of using visibility_of_element_located() you need to use WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img[name='isc_NXicon'][src$='DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png']"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#name='isc_NXicon' and contains(#src, 'DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png')]"))).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

Categories