How to insert a value in text box using Python Selenium - python

I have the following HTML structure and I am trying to use Selenium to enter a value
<div class="form-group justify-content-center d-flex">
<form id="main-form" class="form-inline" action="/pushData" method="post">
<input type="hidden" name="_token" value="bnePp0JmVaVYuaTIAfuVIGT2y7usVssX3vQrAGaz">
<input type="text" id="input-url" class="url-input" name="url" placeholder="Paste URL to shorten">
<button class="button main-btn main-btn_primary" id="button-submit">Cut</button>
</form>
</div>
Here is my code
driver.find_element_by_id("input-url").send_keys("test")
driver.find_element_by_id("button-submit").click()
I want to get this element and enter a value.

Select element by id:
inputElement = driver.find_element_by_id("input-url")
inputElement.send_keys('testing')
Now you can simulate hitting ENTER:
inputElement.send_keys(Keys.ENTER)
or if it is a form you can submit:
inputElement.submit()

Selenium 4.3.0: Deprecated find_element_by_* and find_elements_by_*
are now removed (#10712)
The following code will work:
[..]
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[..]
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'input-url'))).send_keys('test')
button_to_be_clicked = browser.find_element(By.ID, 'button-submit')
button_to_be_clicked.click()

To send a character sequence to the <input> element you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input.url-input#input-url[name='url'][placeholder='Paste URL to shorten']").send_keys("Phonex")
Using xpath:
driver.find_element(By.XPATH, "//input[#class='url-input' and #id='input-url'][#name='url' and #placeholder='Paste URL to shorten']").send_keys("Phonex")
Ideally 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.url-input#input-url[name='url'][placeholder='Paste URL to shorten']"))).send_keys("Phonex")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='url-input' and #id='input-url'][#name='url' and #placeholder='Paste URL to shorten']"))).send_keys("Phonex")
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

Related

How to send text into an address bar which has a data-dojo-attach-point attribute

I'm trying to enter text into an address bar using the following code in python:
driver.find_element_by_xpath('//*[#id="esri_dijit_Search_0_input"]').send_keys('text')
However, it does not register anything when running without debugging. Could you please provide any solutions on how to input text into an address bar with the following html?
<form data-dojo-attach-point="formNode">
<input maxlength="128" autocomplete="off" type="text" tabindex="0" class="searchInput" value="" aria-haspopup="true" id="esri_dijit_Search_0_input" data-dojo-attach-point="inputNode" role="textbox" placeholder="Address or Grid Reference" title="Address or Grid Reference" style="width: 170px;">
</form>
The desired element is a dynamic 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, "input.searchInput[id^='esri_dijit_Search'][title='Address or Grid Reference']"))).send_keys("text")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='searchInput' and starts-with(#id, 'esri_dijit_Search')][#title='Address or Grid Reference']"))).send_keys("text")
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: How to click on a link with no class or ID

How do I click this link using Selenium in Python? I've been able to get to the webpage using Selenium and click other links that have IDs. But this one doesn't have an ID or name.
HTML:
<div ng-repeat="dashboard in $ctrl.dashboards track by $index" class="btn btn-primary ng-scope active" ng-class="{active: dashboard.Dashboard_Id == $ctrl.activeDashboard.Dashboard_Id}" ng-click="$ctrl.dashboardClick(dashboard)" style="">
<span ng-bind="dashboard.Name" class="ng-binding">Hours By Activity</span>
</div>
I've tried this with no luck:
driver.find_elements_by_xpath("//*[contains(text(), 'Hours by Activity')]")
The desired element is an Angular element so to click on the clickable 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, "span.ng-binding[ng-bind='dashboard.Name']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ng-binding' and #ng-bind='dashboard.Name'][text()='Hours By Activity']"))).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

How do I identify the element by the class with Selenium and Python?

<input class="inputDefault-_djjkz input-cIJ7To" name="password" type="password" placeholder="" aria-label="Password" autocomplete="off" maxlength="999" spellcheck="false" value="">```
^this is the HTML
ive been trying something like
login = driver.find_element_by_xpath(".//*[#class='inputDefault-_djjkz input-cIJ7To']/div/input").click()
thanks for any help
You can use the following code :
driver.find_element_by_xpath("//input[#class='inputDefault-_djjkz input-cIJ7To']")
To identify the clickable element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "input[name='password'][aria-label='Password']")
Using xpath:
element = driver.find_element(By.XPATH, "//input[#name='password' and #aria-label='Password']")
Ideally, to identify 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:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password'][aria-label='Password']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='password' and #aria-label='Password']")))
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 won't locate element with any method

I'm having trouble having selenium locate an element on a website (gleam to be exact). Ideally I would like the driver to send keys to an input field, but selenium won't locate it for some reason.
I've already tried locating by ID, Xpath and by name. Any suggestions on how to locate this element?
Here's the html:
<input
id="contestant[name]"
name="name"
ng-model-options="{ debounce: 300 }"
ng-model="contestantState.form.name"
ng-pattern=".*"
placeholder="Alice Smith" required=""
style="width: 246px"
type="text"
class="ng-empty
ng-invalid
ng-invalid-required
ng-valid-pattern
ng-dirty
ng-valid-parse
ng-touched"
>
Try one of these
By.CssSelector("[id*='contestant']")
By.CssSelector("[ng-model='contestantState.form.name']")
By.CssSelector("[name='name']")
Use WebDriverWait to handle the dynamic elements on Web Page.Try following code.
If this code not work check whether your input element inside any iframe.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
inputelement=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'contestant[name]')))
inputelement.send_keys("Apple")
To send a character sequence to the desired element as the element is an Angular element so 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(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-empty.ng-invalid.ng-invalid-required.ng-valid-pattern.ng-dirty.ng-valid-parse.ng-touched[id=\"contestant[name]\"]"))).send_keys("ml2017")
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-dirty ng-valid-parse ng-touched' and #id=\"contestant[name]\"]"))).send_keys("ml2017")

Unable to click on Button using Selenium Python

I'm trying to click on this button to go to the next page, but for some reason, I'm unable to. I tried xpath, css, and class selectors as well as the data-trekkie-id attribute, but nothing I have tried worked. Any help? Code below:
<div class="step__footer" data-step-footer="">
<button name="button" type="submit" class="step__footer__continue-btn btn " data-trekkie-id="continue_to_shipping_method_button" aria-busy="false">
<span class="btn__content">
Continue to shipping method
</span>
</button>
</div>
As per the HTML to invoke click() method on the button with text as Continue to shipping method you need to induce WebDriverWait and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.step__footer__continue-btn.btn[data-trekkie-id='continue_to_shipping_method_button']>span.btn__content"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='step__footer__continue-btn btn' and #data-trekkie-id='continue_to_shipping_method_button']/span[#class='btn__content']"))).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
This should work:
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(#class, 'step__footer__continue-btn')]").click()

Categories

Resources