如何选择这个textarea?

时间:2018-05-25 13:07:33

标签: selenium selenium-webdriver css-selectors

我需要使用Selenium将光标放在textarea元素中。该元素没有ID。 我试过了:

def get_data(batchsize=None):
    conn = psycopg2.connect('database parameter')
    cursor = conn.cursor()
    query = 'select * from table'
    cursor.execute(query)
    if not batchsize:
        result = cursor.fetchall()
        return result
    else:
        while True:
            result = cursor.fetchmany(batchsize)
            if not result:
                break
            yield result

if __name__ == '__main__':
    data = get_data()

并抛出NoSuchElementException

enter image description here

Web Page and underlying code

1 个答案:

答案 0 :(得分:4)

textarea没有@class - 它是祖先div的属性。请尝试以下代码:

eventDriver.findElement(By.cssSelector("div.CodeMirror textarea")).click();

或使用ExplicitWait:

WebDriverWait wait = new WebDriverWait(eventDriver,10);

wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.CodeMirror textarea"))).click();

更新

WebElement element = eventDriver.findElement(By.cssSelector("div.CodeMirror textarea"))
JavascriptExecutor jse = ((JavascriptExecutor)eventDriver);
jse.executeScript("arguments[0].click()", element);