Selenium send_keys 奇怪問題

本帖最後由 bongbong3481 於 2023-10-29 07:11 編輯

用python selenium send_keys send 幾個數字去網頁,發覺偶爾會send 吾到部份數字 ,有冇人知咩原因?
(另用其它方法解決到eg.actionchain 或者pyautogui sendkey ,但吾明點解)

Selenium send_keys strange issue. I use python selenium send_keys to send multiple numbers to a webpage. I found that sometimes I only get some of the numbers. What could be the reason? (I have tried other methods to solve the issue, such as actionchain or pyautogui sendkey, but I don't know how to fix it.)

Based on your question and the search results, it seems that you are experiencing some strange issues when using the send_keys method in Python's Selenium, where only some of the numbers are being sent. This could be caused by several reasons.

String input speed too fast: The send_keys method in Selenium simulates keyboard input by sending each character of the string one by one. If the input speed is too fast, some characters may be lost. You can try adding some delays between each character to ensure that each character is correctly inputted.

Web element focus issue: Sometimes, the web element may not receive the focus correctly, which can cause the send_keys method to fail in inputting characters into that element. You can try using other methods like ActionChains or pyautogui's sendkey to simulate keyboard input and ensure that the web element has received the focus.

Special character handling issue: Sometimes, special characters may be interpreted differently, resulting in incorrect character input. You can try using different character encodings or escape characters to handle special characters and ensure that they are correctly inputted.

In summary, you can try the following solutions to address the issue you are facing:

Add delays: Add some delays between each character to ensure that each character is correctly inputted. You can use the time.sleep() method to implement the delays.

Use ActionChains: Use the ActionChains class to simulate keyboard input and ensure that the web element has received the focus. You can use the send_keys method of ActionChains to simulate keyboard input.

Handle special characters: If your string contains special characters, try using different character encodings or escape characters to handle them and ensure that they are correctly inputted.

TOP

回覆 2# s20012797


    原來是字符間的輸入速度太快, 睇黎想懶d 都吾得,send_keys 特別多奇怪問題

TOP

some_element.set_attribute('value','your text') 唔得?

TOP

some_element.set_attribute('value','your text') 唔得?
epcsub 發表於 2023-10-30 07:03


可能吾識用,出左句,webelement object has no attribute ' set_attribute  '。

TOP

可能吾識用,出左句,webelement object has no attribute ' set_attribute  '。
bongbong3481 發表於 2023/10/30 21:43


There are a few reasons why Selenium send_keys might not be working as expected.

The element you are trying to send keys to may not be in focus.
The element may be disabled or read-only.
There may be a JavaScript error preventing the send_keys command from working.
To troubleshoot this issue, you can try the following:

Make sure the element you are trying to send keys to is in focus. You can do this by using the driver.switch_to.active_element() command.

Make sure the element is not disabled or read-only. You can do this by checking the disabled and readonly attributes of the element.

Check the browser's JavaScript console for any errors. If there are any errors, you can try to fix them or work around them.

If you are still having trouble, you can try using a different method to send keys to the element, such as the actionChains or pyautogui libraries.

Finally, you can also try using the some_element.set_attribute('value','your text') method to set the value of the element. However, this method is not always reliable and may not work in all cases.

TOP

回覆 4# epcsub


    Sorry,係我搞錯。

TOP

回覆 6# s20012797


    我其實都用左actionchain ,都有試pyaugui 沒問題,最終選用actionchain

TOP