Python selenium将密钥发送到textarea

时间:2016-04-14 04:50:24

标签: python selenium

我使用Python 3.4.4访问具有textarea的网站(https://readability-score.com/),该网站在添加新值时会动态更新。

我试图将字符串输入到该textarea框中,但它并不适合我。

以下是我尝试的代码:

driver = webdriver.Firefox()
driver.wait = WebDriverWait(driver, 2)
URL = "https://readability-score.com/"

text = "Hello hello hello, this is a test"

driver.get(URL)
time.sleep(2)
driver.find_element_by_id("text_to_score").clear()
driver.find_element_by_id("text_to_score").send_keys(text)
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").clear()
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").send_keys(text)

问题是selenium驱动程序找不到textarea来发送密钥。我认为它能够清除它(因为我可以逐字地看到当你进入页面时文本被清除)但是没有文字可以进入。有人会对此有所了解吗?我按照在线指南进行了操作,但我觉得我已经尝试了列出的所有选项(http://selenium-python.readthedocs.org/)。谢谢。

3 个答案:

答案 0 :(得分:3)

You can explicitly wait直到textarea出现。

boolean okToProcess = true;
    String message = "";
    int colourInt;

    String input = in.getText();
    String direction = input.substring(0, 1);
    String distance = input.substring(1);

    double distanceAsDouble = 0;



    if (direction.equals("n"))
        t.heading(0);
    else if (direction.equals("ne"))
        t.heading(45);
    else if (direction.equals("e"))
        t.heading(90);
    else if (direction.equals("se"))
        t.heading(135);
    else if (direction.equals("s"))
        t.heading(180);
    else if (direction.equals("sw"))
        t.heading(225);
    else if (direction.equals("w"))
        t.heading(270);
    else if (direction.equals("nw"))
        t.heading(315);
    else {
        okToProcess = false;
        message += "bad direction: " + direction + " ";
    }


    if (isNumeric(distance)) {
        distanceAsDouble = Double.parseDouble(distance);
    }
    else{
        okToProcess = false;
        message += "bad distance: " + distance;
    }

    if (okToProcess) {
        if (!EtchASketchClipped(t, distanceAsDouble)) {
            t.setLineWidth(3);

答案 1 :(得分:2)

“ send_keys”功能仅会键入您输入的字符串,而不会“发送”键,因为这看起来似乎违反直觉。提交文本区域的正确方法是使用简单的

driver.find_element_by_id("text_to_score").submit()

OR

textarea = driver.find_element_by_id("text_to_score")
textarea.submit()

答案 2 :(得分:-1)

这对我有用,试试看看?

from selenium import webdriver
import time

driver = webdriver.Chrome()
#driver.wait = WebDriverWait(driver, 2)
URL = "https://readability-score.com/"

text = "Hello hello hello, this is a test"

driver.get(URL)
time.sleep(5)
driver.find_element_by_id("text_to_score").clear()
driver.find_element_by_id("text_to_score").send_keys(text)
print "Here"
time.sleep(4)
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").clear()
#driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[1]/form/fieldset/textarea").send_keys(text)

driver.close()

你可以摆脱driver.close()来保持窗口打开。