AttributeError: 'WebElement'オブジェクトに属性 'send_keys'がありません



Attributeerror Webelementobject Has No Attributesend_keys



これは問題のないコードです:チーズのGoogle検索を開いて終了するために使用されます

from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 # Create a new instance of the Firefox driver driver = webdriver.Firefox() # go to the google home page driver.get('http://www.google.com') # the page is ajaxy so the title is originally this: print driver.title # find the element that's name attribute is q (the google search box) inputElement = driver.find_element_by_name('q') # type in the search inputElement.send_keys('cheese!') # submit the form (although google automatically searches now without submitting) inputElement.submit() try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait(driver, 10).until(EC.title_contains('cheese!')) # You should see 'cheese! - Google Search' print driver.title finally: driver.quit()

問題のコード:Baiduホームページ検索を開いて終了します




from selenium import webdriver driver=webdriver.Firefox() driver.get('http://www.baidu.com') element = driver.find_element_by_id('kw1') element.send_keys ( 'test') # here given driver.close()

エラー:AttributeError: 'WebElement'オブジェクトに属性がありません 'send_keys'

ヒントsendKeysはインポートパッケージに見えません。



現在のコード:

from selenium import webdriver from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.common.action_chains import ActionChains driver=webdriver.Firefox() driver.get('http://www.baidu.com') ele = driver.find_element_by_name('wd') print (ele) ele.send_keys('test') btn = driver.findElement(By.id('su1')) btn.click() driver.close()

エラープロンプト:

トレースバック(最後の最後の呼び出し):
ファイル 'D: pcode 24.py'、9行目、
ele.send_keys( 'test')
ファイル 'D: Python27 lib site-packages selenium webdriver remote webelement.py'、
send_keysの293行目
self._execute(Command.SEND_KEYS_TO_ELEMENT、{'value':入力})
ファイル 'D: Python27 lib site-packages selenium webdriver remote webelement.py'、
370行目、_execute
self._parent.execute(command、params)を返します
ファイル 'D: Python27 lib site-packages selenium webdriver remote webdriver.py'、l
ine 166、実行中
self.error_handler.check_response(response)
ファイル 'D: Python27 lib site-packages selenium webdriver remote errorhandler.py'
、164行目、check_response
exception_class(message、screen、stacktrace)を発生させます
selenium.common.exceptions.ElementNotVisibleException:メッセージ:u '要素はありません
現在表示されているため、スタックトレースと相互作用しない可能性があります。



正しいコードは次のとおりです。

from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 import time # Create a new instance of the Firefox driver browser = webdriver.Firefox() # open baidu.com browser.get('http://www.baidu.com') # sleep 2 secs time.sleep(2) #clean the enter text browser.find_element_by_id('kw1').clear() #enter something browser.find_element_by_id('kw1').send_keys('selenium') #submit browser.find_element_by_id('su1').click() # sleep 2 secs time.sleep(2) try: # we have to wait for the page to refresh, the last thing that seems to be updated is the title WebDriverWait(browser, 10).until(EC.title_contains('selenium')) # You should see 'selenium - Baidu search' print browser.title finally: browser.quit()

別の書き方:

from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 import time # Create a new instance of the Firefox driver browser = webdriver.Firefox() # open baidu.com browser.get('http://www.baidu.com') # sleep 2 secs time.sleep(3) #clean the enter text browser.find_element_by_id('kw1').clear() #enter something browser.find_element_by_id('kw1').send_keys('selenium') #submit browser.find_element_by_id('su1').click() # sleep 2 secs time.sleep(2) #wait for the page while True: #if fresh contains = browser.title.find('selenium') >= 0 if (contains): break else: sleep(1) #quit browser.quit()