Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the input field value using Selenium and Python

I am trying to change the input field value in a form input, but unable to do so.

Input value is at this URL

Email: [email protected] PW: Testing@123

On the page form appears after these actions:

1. on left side click on "Sell Order" red button in Dashboard widget
2. a popup will appear on click, select "Order Type" as "Limit"
3. a new formfield appears, named "Limit Price" 

Required Field When I try to change it using selenium or even if JS, it doesn't seem to change when we submit the form by pressing "Send Order"

My code trails are:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time

driver.get(url)
driver.find_element_by_css_selector('#gwt-uid-770 > span > span > input').send_keys('6700')

I have also tried

driver.execute_script("document.querySelector('#gwt-uid-770 > span > span > input').value=6700")

But still when I submit it doesn't seem to change.

like image 386
curious_nustian Avatar asked Jun 11 '20 09:06

curious_nustian


People also ask

How do you input a field value in Python?

We can get the value of an input box with Selenium webdriver. The get_attribute() method is capable of obtaining the value we have entered in an input box. To get the value, we have to pass value as a parameter to the method.

How do I input Selenium in Python?

We can send keyboard input to a textbox on a webpage in Selenium webdriver in Python using the method send_keys. The text to be entered is passed as a parameter to that method. To perform keyboard actions, we can also use the send_keys method and then pass the class Keys.

How do I edit a form in Selenium?

Selenium can be used to input text to an edit box. An edit box is represented by the input tag and its type attribute should have the value as text. It can be identified with any of the locators like - id, class, name, css, xpath and tagname. To input a value into an edit box, we have to use the method send_keys.


2 Answers

To perform the following steps:

  1. On left side click on "Sell Order" red button in Dashboard widget
  2. A popup will appear on click, select "Order Type" as "Limit"
  3. A new formfield appears, named "Limit Price"
  4. Send the value 10000 to the <input> field.

You have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://primexbt.com/id/sign-in?redirect=%2Fmy%2Ftrade')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[formcontrolname='email']"))).send_keys("[email protected]")
    driver.find_element_by_css_selector("input[formcontrolname='password']").send_keys("Testing@123")
    driver.find_element_by_css_selector("span.mat-button-wrapper").click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.popup-close"))).click()
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.ng-star-inserted")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.price.price-bid"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='selectBox--label' and text()='Market']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='dropDown--list']//li[contains(., 'Limit')]"))).click()
    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Limit Price']//following::span[1]//span[@class='numericStepper--input']//input[@class='gwt-TextBox']")))
    ActionChains(driver).click(element).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).send_keys("10000").perform()
    
  • Note: At the time of constructing the answer:

Entry Price you set must be higher or equal to 9786.7

  • Browser Snapshot:

primaxbt

like image 140
undetected Selenium Avatar answered Sep 17 '22 08:09

undetected Selenium


While inspecting the require element i found that there is an iframe on which the content wrapped.

enter image description here

So to deal with frames in selenium first you need to switch into right iframe and then it allows you to interact with the elements.

  1. Switch using iframe indexes

    driver.switch_to.frame(0)
    
  2. Switch using iframe element

    main_frame = driver.find_element_by_css_selector('iframe.ng-star-inserted')
    driver.switch_to.frame(main_frame)
    
  3. Switch using iframe name

    driver.switch_to.frame('frame name') #make sure name attribute available in frame tag
    

This is how you can use Explicit wait condition to switch into it

WebDriverWait(driver, 45).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.ng-star-inserted")))

And use this locator to enter text in texxtbox

driver.find_element_by_xpath("//span[contains(.,'Limit Price')]/following-sibling::span//input").send_keys('6700')
like image 25
NarendraR Avatar answered Sep 21 '22 08:09

NarendraR