Sainita
Sainita

Reputation: 362

AttributeError: 'WebElement' object has no attribute 'sendKeys'

I am trying to select all Text Fields on a webform and delete it. The site is : https://onlinehtmleditor.dev/

Attached Image : enter image description here This is my code:

w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://onlinehtmleditor.dev/")
time.sleep(4)
switch_to_html ='//*[@id="ckeditor-4-output-button"]'
paste_code = '//*[@id="ckeditor-4-output"]/div/div[6]/div[1]/div/div'
element = w.find_element_by_xpath(switch_to_html).click()
time.sleep(2)
# element = w.find_element_by_xpath(paste_code).send_keys("Hi")
clickElement = w.find_element_by_css_selector(".CodeMirror-lines")
time.sleep(1)
clickElement.click()
clickElement.sendKeys(Keys.CONTROL + "a")
clickElement.sendKeys(Keys.DELETE)

Getting Issue in this Line :

clickElement.sendKeys(Keys.CONTROL + "a")
AttributeError: 'WebElement' object has no attribute 'sendKeys'

How to fix this Issue, please guide me

Upvotes: 2

Views: 689

Answers (2)

Mel Davila
Mel Davila

Reputation: 1

In version v23 Calling 2nd time the sendKeys in a WebElement has not efect.

WebElement webField = $(TextFieldElement.class).waitForFirst();
webField.sendKeys("Vaadiner");
webField.sendKeys("TEST");

The field do not change the value. must be "TEST" but continue with "Vaadiner". So if you do so:

webField.sendKeys(Keys.CONTROL + "a");
webField.sendKeys(Keys.DELETE);

Has not effect.

Try to do some thing as :

List<WebElement> list  =getDriver().findElements(By.className("CodeMirror-lines"));
for (final WebElement webElement : list) 
     if (webElement instanceof TextFieldElement)
        ((TextFieldElement) webElement).clear();

Upvotes: 0

Prophet
Prophet

Reputation: 33351

In Python Selenium the method to send a text to a web element is send_keys.
sendKeys is a Java Selenium method.
So instead of

clickElement.sendKeys(Keys.CONTROL + "a")
clickElement.sendKeys(Keys.DELETE)

use

clickElement.send_keys(Keys.CONTROL + "a")
clickElement.send_keys(Keys.DELETE)

Upvotes: 3

Related Questions