1

I want to add the text via .send_keys() to the text field input, but after the image file.

However, for some reasons Selenium adds the text always before the image file, even though I added the image file before sending the text input.

For example:

image_button.send_keys(IMAGE_FILEPATH)
textfield.send_keys("text to insert")

The image_button is a input tag I obtained via driver instance, and it is located before the text field. After I sending the image file via .send_keys() method, it shows in the text field.

However, I'm not sure how I can add the text after the image on the text field. Is it possible?


Below is the screenshot and the code is something like the following:

front_textfield, back_textfield = driver.find_elements_by_xpath("//div[@class='richEditor']")
image_front_button, image_back_button = driver.find_elements_by_xpath("//input[@accept='image/*']")
audio_front_button, audio_back_button = driver.find_elements_by_xpath("//input[@accept='audio/*']")
image_front_button.send_keys(image_file)
front_textfield.send_keys(text_front)
audio_front_button.send_keys(audio_file)

enter image description here

I tried running each code one-by-one on my Jupyter notebook, but it failed anyway.

Here is the event associated to the text field:

enter image description here


Hmmm one of the workaround is add the following code, which moves the cursor in the text field.

image_front_button.send_keys(image_file)
for i in range(2):
    front_textfield.send_keys(Keys.ARROW_DOWN)
    front_textfield.send_keys(Keys.ARROW_RIGHT)
front_textfield.send_keys(text_front)
Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • Can you try waiting for the presence of `textfield` using Expected Conditions before running the `textfield.send_keys("text to insert")`. I believe either the image submission event is not triggering or you might be trying to enter the text too early (I am before file upload complete). – supputuri May 24 '19 at 12:40
  • @supputuri I did, but it didn't work, either. I found out that the reason is likely because when I added the image manually and after I tapped the text field, I could not start to type in my text. Only after I tapped in the empty space of the text with my cursor was I able to type in my text. This is likely the cause of this problem, I think. Not sure how I can deal with it on Selenium yet, though. Any idea? – Blaszard May 24 '19 at 12:45
  • Can you please check the events associated with the textfield by following the approach mentioned [here](https://stackoverflow.com/questions/55977388/textbox-events/55978587#55978587). – supputuri May 24 '19 at 12:51
  • Can you please add the screenshot of the events, I can give little piece of code that you can try using the event to simulate the textfield tapped event. – supputuri May 24 '19 at 12:53
  • Put a breakpoint right before this code and step through it. It shouldn't be putting the text in a different order than your code. That seems highly unlikely, if not impossible. Code is not going to run out of order. – JeffC May 24 '19 at 13:04
  • @supputuri I added the screenshot and more code, including getting the relevant HTML via XPath. I'm going to read the link. – Blaszard May 24 '19 at 13:12
  • @JeffC That's what I tried. I added more information, let me know if something is unclear. – Blaszard May 24 '19 at 13:13
  • Can you please let me know the events associated to the text filed. – supputuri May 24 '19 at 13:17
  • I see what you mean now. The picture helped a lot. Did you try clicking in the edit box, hit CTRL+END, and then sendkeys? – JeffC May 24 '19 at 13:22
  • @supputuri If I understand you correctly, I added the screenshot that includes all the events. – Blaszard May 24 '19 at 13:24
  • @JeffC I tried adding `front_textfield.send_keys(Keys.COMMAND + Keys.ARROW_DOWN)` before inserting the text but with no luck (I'm on macOS). – Blaszard May 24 '19 at 13:31
  • Have you tried inserting the text first and then the image? It seems like whatever is done last ends up at the front. – JeffC May 24 '19 at 13:35
  • @JeffC Yes but in that case the text is inserted first. Also, the audio file, which I added last, is always inserted last. – Blaszard May 24 '19 at 13:44
  • @Blaszard I believe you should have an `input` tag within the div that you shared in screenshot. Can you expand that node and check if there is an input. If not, check if you see the input after clicking the div. – supputuri May 24 '19 at 13:49
  • @supputuri `input` does exist on the image and audio button, but does not on the text field or any other places. – Blaszard May 24 '19 at 14:03
  • Can you please try the below code before entering the text `driver.execute_script( "arguments[0].dispatchEvent(new Event('touchstart', {'bubbles': true,'cancelable': true}));", element)`. If that does not work then try with other events like blur, change, click – supputuri May 24 '19 at 14:08
  • @supputuri All of them are not working... – Blaszard May 24 '19 at 14:11
  • Is it a public url? If yes, can you please share it. – supputuri May 24 '19 at 14:12
  • @supputuri It is a service called [AnkiApp (Web)](https://web.ankiapp.com/). Everyone can use it, but needs to create an account and login. It is a SRS software that lets you memorize words of a foreign language (or anything you want). I want to automate the process of adding a new vocabulary, FYI. – Blaszard May 24 '19 at 14:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193893/discussion-between-supputuri-and-blaszard). – supputuri May 24 '19 at 14:16
  • May be you need to focus on the text editor first. Can you try moveToElement method of actions class or scrollIntoView function of javascript using JavascriptExecutor? – Sachin May 24 '19 at 21:27

0 Answers0