6

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: Vela.admi@gmail.com 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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
curious_nustian
  • 596
  • 2
  • 7
  • 22

2 Answers2

4

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("Vela.admi@gmail.com")
    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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • can you explain what `WebDriverWait(driver, 10).until(EC.element_to_be_clickable` does. In 10 to 12 trails, it failed 8-9 times, before it, i used time.sleep which showed better results – curious_nustian Jun 12 '20 at 10:46
  • 1
    @curious_nustian See the discussion [How to get rid of the hardcoded sleep()?](https://stackoverflow.com/questions/54637664/how-to-get-rid-of-the-hardcoded-sleep/54637793#54637793) to understand why not to use `time.sleep()` and [WebDriverWait not working as expected](https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808) discussion for `element_to_be_clickable` – undetected Selenium Jun 12 '20 at 10:53
  • what can be cause of `raise TimeoutException(message, screen, stacktrace)` despite the element is visible – curious_nustian Jun 12 '20 at 11:06
  • 1
    _WebDriverWait_ ignores [NoSuchElementException](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294) and on timeout raises [TimeoutException](https://stackoverflow.com/questions/48916783/python-selenium-chromedriver-timeoutexception/48920865#48920865) – undetected Selenium Jun 12 '20 at 11:10
2

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')
NarendraR
  • 7,577
  • 10
  • 44
  • 82