Python脚本在写入初始一组单词后崩溃。

huangapple go评论55阅读模式
英文:

Python script is crashing after writing initial set of words

问题

尝试编写一个自动化键盘速度测试网站的程序。然而,在网站首次打开时显示的第一组单词后,程序崩溃了。之后它就不再输入任何内容。我尝试了多种解决方案,但都没有效果。

首先,我认为浏览器可能阻止了自动输入。为了模拟人类的输入,我在每行代码之间添加了随机延迟。

然后我认为可能是网站在首次显示一行单词后更新了内容,尝试使用以下代码更新整个代码块,但没有帮助。

基本上,我解决了这个问题。我在下面添加了完整的代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

try:
    driver = webdriver.Firefox()
    driver.get("https://10fastfingers.com/typing-test/english")

    # Wait for the page to fully load
    time.sleep(6)

    # Find the text input element on the page
    text_input = driver.find_element("id", "inputfield")

    # Find all the words to type
    words = driver.find_elements("xpath", "//span[@wordnr]")

    # Type each word into the input field
    for word in words:
        text_to_type = word.text
        for character in text_to_type:
            text_input.send_keys(character)
            time.sleep(0)
        text_input.send_keys(" ")
        time.sleep(0)

    # Submit the form to complete the typing test
    text_input.send_keys(Keys.RETURN)

except Exception as e:
    print("An error occurred:", e)

希望这次提供的代码能够帮到您。

英文:

I am attempting to write a program that automates the keyboard speed test website. However, the program crashes after writing the first initial set of words which is displayed when the website is first opened. After that, it does not write anything. I have tried multiple solutions, but none of them have been effective.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

try:
    driver = webdriver.Firefox()
    driver.get("https://10fastfingers.com/typing-test/english")

    # Wait for the page to fully load
    time.sleep(6)

    # Find the text input element on the page
    text_input = driver.find_element("id", "inputfield")

    current_word = driver.find_element("id", "row1")
    print(type(driver))  # <-- Add this line
    # Type the words into the input field
    while True:
        text_to_type = current_word.text

        for character in text_to_type:
            text_input.send_keys(character)
            time.sleep(0.1)

        # Check if the next sibling element exists
        next_word = current_word.find_element_by_xpath("following-sibling::span[1]")
        if not next_word:
            break

        current_word = next_word

    # Submit the form to complete the typing test
    text_input.send_keys(Keys.RETURN)

except Exception as e:
    print("An error occurred:", e)

finally:
    # Close the web browser
  driver.quit()

First I thought browser is blocking automatic typing. In order to mimic human writing I added random delay between each code.

for word in text_to_type:
        for character in word:
            text_input.send_keys(character)
            time.sleep(random.uniform(0.05, 0.1))
        text_input.send_keys(Keys.SPACE)
        time.sleep(random.uniform(0.5, 1.0))
    

Then I thought maybe website updates itself after first initial line and tried to update whole block with this code but it didn't help.

while True:
        # Get the next word to type
        word_elements = driver.find_elements("css selector", "#row1 span")
        if not word_elements:
            break
        word = word_elements[0].text

So basically I could able to solve this problem. I am adding full code below.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

try:
    driver = webdriver.Firefox()
    driver.get("https://10fastfingers.com/typing-test/english")

    # Wait for the page to fully load
    time.sleep(6)

    # Find the text input element on the page
    text_input = driver.find_element("id", "inputfield")

    # Find all the words to type
    words = driver.find_elements("xpath", "//span[@wordnr]")

    # Type each word into the input field
    for word in words:
        text_to_type = word.text
        for character in text_to_type:
            text_input.send_keys(character)
            time.sleep(0)
        text_input.send_keys(" ")
        time.sleep(0)

    # Submit the form to complete the typing test
    text_input.send_keys(Keys.RETURN)

except Exception as e:
    print("An error occurred:", e)

    #finally:
        # Close the web browser
     #   driver.quit()

答案1

得分: 0

driver.find_element("id", "row1")获取的单词并不是完整的单词列表,即使在测试开始之前也是如此。看起来该结果只包括在"words" div中可见的单词。页面上还有更多隐藏的单词,但可以通过检查页面找到。

您上次的尝试应该可以工作。当我运行driver.find_elements("css selector", "#row1 span")时,我得到一个包含您要查找的单词的约380个元素的列表。对于您的键入实现进行一些小的调整以在每个单词之间添加空格,它就可以正常工作。

英文:

The words you get from driver.find_element("id", "row1") aren't the complete list of words, even before the test starts. It looks like that result only includes words that are visible in the "words" div. There are more words hidden on the page but can be found by inspecting the page.

Your last attempt should have worked. I get a list of ~380 elements that contain the words you are looking for when I run driver.find_elements("css selector", "#row1 span"). Some small adjustments to your typing implementatino to add in the spaces between each word and it works just fine.

huangapple
  • 本文由 发表于 2023年2月8日 23:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定