我的代码:
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By driver=webdriver.Firefox() driver.get("http://www.58yumi.com/") driver.find_element_by_id("UserName").send_keys("XXXXXXX") driver.find_element_by_id("Password").send_keys( "XXXXXX") driver.find_element_by_xpath("//*[contains(@type,'submit')]").click() driver.get("http://www.58yumi.com/user_jiexi.htm") driver.find_element_by_id("cznr").sendKeys("XXXX.com|forcname|CNAME|forcname.XXXX.com.a.bdydns.com|default"); ......
我在 element_by_id(“cznr”) 中收到错误:
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'WebElement' object has no attribute 'sendKeys'
HTML:
<textarea id="cznr" onkeyup="czhang();" name="ymlb" cols="60" class="inputs2" rows="10"></textarea>
帮助....如何在文本区域中输入数据?
sendKeys()将以下行中的基于Java的方法替换为基于Python的方法:send_keys()
sendKeys()
send_keys()
driver.find_element_by_id("cznr").sendKeys("XXXX.com|forcname|CNAME|forcname.XXXX.com.a.bdydns.com|default");
问题出在方法上sendKeys,该方法应该send_keys在 Selenium 中用于在文本区域(或任何输入字段)中输入文本。以下是更正后的代码:
sendKeys
send_keys
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By # Initialize the driver and open the website driver = webdriver.Firefox() driver.get("http://www.58yumi.com/") # Log in driver.find_element(By.ID, "UserName").send_keys("XXXXXXX") driver.find_element(By.ID, "Password").send_keys("XXXXXX") driver.find_element(By.XPATH, "//*[contains(@type,'submit')]").click() # Navigate to the user page and fill in the textarea driver.get("http://www.58yumi.com/user_jiexi.htm") textarea = driver.find_element(By.ID, "cznr") textarea.send_keys("XXXX.com|forcname|CNAME|forcname.XXXX.com.a.bdydns.com|default")
By.ID
By.XPATH
<textarea>这应该允许您毫无问题地将数据输入到元素中。
<textarea>