简体   繁体   中英

Can not type in a text box using python selenium

I'm trying to send keys to the following textarea html using python selenium:

<div class="spk-c spH-d"><div id="gwt-uid-23" class="sppb-a">
<div class="sppb-b spk-b">For example, flowers or used cars</div>
<textarea rows="2" aria-labelledby="gwt-uid-64 gwt-uid-23" id="gwt-debug-keywords-text-area" class="spk-a sppb-c">
</textarea>
</div> 
<div role="alert" class="error" style="display:none"> Input contains a keyword that is too long. </div> <div class="error" style="display:none">Your product or service description can't exceed 1,000 words. Remove some words and try again.
</div>
</div>

and I get this error:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:

here is my code:

textarea='textarea[id="gwt-debug-keywords-text-area"]'
element = WebDriverWait(driver, 15).until(lambda driver : driver.find_element_by_css_selector(textarea))
driver.find_element_by_css_selector(textarea).send_keys('plumbers')

Can you please help me out? It's been bugging me for a while. The html is from google keyword planner. Thank you

You can use expected conditions to wait for the text box visibility

WebDriverWait(driver, 15).until(expected_conditions.visibility_of_element_located((By.ID, 'gwt-debug-keywords-text-area'))).send_keys('plumbers')

Edit

You need to switch to the iframe with the textarea in order to interact with it

# by the frame id attribute
driver.switch_to.frame(id)

# by the frame name attribute
driver.switch_to.frame(name)

# by the frame webelement
frame = drive.find_element(...) # locate the frame
driver.switch_to.frame(frame)

And to switch back

driver.switch_to.default_content()

The problem may not be with your selenium code, but the workflow for using the tool. I am not familiar with this adwords keyword planner, but when I did a Google search for the id of that text area and selenium, I came across some github code that looks like it is enabling the text area first:

browser.find_element_by_id("gwt-debug-splash-panel-find-keywords-selection-input").click()
browser.find_element_by_xpath("//textarea[@id='gwt-debug-keywords-text-area']").send_keys(keyword)

Try to use JS code to make element visible before sending text:

driver.execute_script("document.getElementById('gwt-debug-keywords-text-area').style.visibility = 'visible';")
driver.execute_script("document.getElementById('gwt-debug-keywords-text-area').style.display='bloc‌​k';")

Let me know if any exceptions occurs

UPDATE

If you want to send text to text area using JS you might need to execute following:

driver.execute_script("document.getElementById('gwt-debug-keywords-text-area').innerHTML='Here is some text';")

or

driver.execute_script("document.getElementById('gwt-debug-keywords-text-area').value='Here is some text';")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM