0

Currently im working on automating a website with selenium python. I have this problem whereby when i enter J590 into the target textbox MANUALLY, i will have to click anywhere on the website or pressing tab which than will refresh the website and an option will be available in a dropdown box. However, selenium does not seem to be refreshing the page for me.

I tried to use click() before and after sending J590 into the textbox,i also tried using send_keys(Keys.TAB) after sending J590 but none is working.

Here is the code i used to send inputs

driver.execute_script("document.getElementById('CustCd').value+="+ dumps(jcode))

Here is the code i tried after sending input

driver.find_element_by_xpath('//*[@id="Item"]/form/center/input[2]').click()
or
driver.find_element_by_xpath('//*[@id="CustCd"]').send_keys(Keys.TAB)

May i know why is selenium acting this way and is there a solution to it.

It'sNotMe
  • 1,184
  • 1
  • 9
  • 29
JiaHao Wang
  • 101
  • 3
  • 13
  • 2
    It's hard to tell but are you sure you're not clicking on the same input element, don't you need to click on a different element to trigger the refresh? – Madison Courto Sep 10 '19 at 01:16
  • 1
    @MadisonCourto ah yes, i clicked on a different element. I forget to change the xpath. – JiaHao Wang Sep 10 '19 at 01:18
  • 1
    It's working now? – Madison Courto Sep 10 '19 at 01:19
  • 2
    not sure why you are using js to input the value rather using the send_keys. I doubt the keyboard events associated the element are not triggering when you use the js. If js is the only option to set the values, then you have to dispatch the corresponding event to trigger the change. Refer to [this](https://stackoverflow.com/questions/55977388/textbox-events/55978587#55978587) answer to know the associated event for the element. – supputuri Sep 10 '19 at 01:32
  • @MadisonCourto no its not working – JiaHao Wang Sep 10 '19 at 01:42
  • @supputuri because send_keys does not capture all of my text so i had to use js – JiaHao Wang Sep 10 '19 at 01:43
  • have you checked the associated event to the element? – supputuri Sep 10 '19 at 02:29
  • @supputuri yes i checked, but how do i call the event in python? – JiaHao Wang Sep 10 '19 at 02:33
  • let's say you want to trigger `onchange` event on an element, then you should do `ele = driver.find_element_by_xxxx(yyy)` and then `driver.executeScript("arguments[0].dispatchEvent(new Event('change', {'bubbles': true,'cancelable': true}));",ele)`. Make sure you returned the correct element. – supputuri Sep 10 '19 at 02:53

1 Answers1

1

Seems to be the keyboard/onchange event are triggering when you tab out or click on another element in the page as you are using js to enter the data. So, you have to dispatch (simulate) the corresponding event.

Refer to this post to know the associated event(s) to the element.

Let's say if you want to trigger onchange event on an element, then you should get the element first.

ele = driver.find_element_by_xxxx(yyy)

then send the event to the element using the js as shown below.

driver.executeScript("arguments[0].dispatchEvent(new Event('change', {'bubbles': true,'cancelable': true}));",ele)

Make sure you returned the correct element. And change the event name accordingly.

supputuri
  • 13,644
  • 2
  • 21
  • 39