1

I know I am doing something wrong. I need to create software to test a website. send_keys is working just fine for the rest of my code but I am not sure what I am doing wrong in this section. I have tried everything I can think of. I am very new to python and selenium so it's probably something silly.

Things I have tried:

1.

elem =  lambda: driver.find_element_by_xpath('//textarea[@aria-label="Comment"]').click()
elem().click()
elem.send_keys("this is a comment")
elem.send_keys(Keys.RETURN)

2.

elem =  lambda: driver.find_element_by_xpath('//span[@aria-label="Comment"]').click()
elem().click()
elem.send_keys("this is a comment")
elem.send_keys(Keys.RETURN)

3.

com_elem = driver.find_element_by_xpath('//textarea[@aria-label="Add a comment…"]')
com_elem.clear()
com_elem.send_keys("comment")

I have tried every combination I can think of and I know it needs to match the HTML but I have still tried it with lambda and without, with span and textarea (since it has a button you press that places your cursor in the text box) I do not know what else to try.

All it needs to do is click the text box, add words and hit enter.

this is HTML for the text box it needs to go inside of:

<form class="X7cDz" method="POST">

<textarea aria-label="Add a comment…" placeholder="Add a comment…" class="Ypffh" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>

<button class="sqdOP yWX7d    y3zKF     " type="submit">
Post
</button> 

</form>

This is HTML for the button that it can click and the cursor will go in the box:

<span class="_15y0l">
<button class="dCJp8 afkep">
<span aria-label="Comment" class="glyphsSpriteComment__outline__24__grey_9 u-__7"> 
</span>
</button>
</span>

This part of the website works perfectly. I know because I have tested it by hand.

2
  • Does it throw any exception or the code completes without errors but is doing nothing? Commented Oct 9, 2019 at 18:45
  • @AlexeyR It runs but it does everything else except leave the comment. It doesn't even write it in the box or anything. Commented Oct 10, 2019 at 0:27

2 Answers 2

1

The desired element is a JavaScript enabled element so to invoke send_keys() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[aria-label^='Add a comment'][placeholder^='Add a comment']")))
    elem.send_keys("Daniela Ciro")
    elem.send_keys(Keys.RETURN)
    
  • Using XPATH:

    elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[starts-with(@aria-label, 'Add a comment') and starts-with(@placeholder, 'Add a comment')]")))
    elem.send_keys("Daniela Ciro")
    elem.send_keys(Keys.RETURN)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Sign up to request clarification or add additional context in comments.

2 Comments

elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[aria-label^='Add a comment'][placeholder^='Add a comment']"))).click() and elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[aria-label^='Add a comment'][placeholder^='Add a comment']"))) elem.click()
The text box becomes enabled when you click it which is why I was trying to click it first. Do I need to still click it before I do 'send_keys'? If so how would you do that? I tried what you sent me and it's still not working. i did this:
0

I don't think you need to use lambda to complete this task. You're on the right track, you need to first identify the path of the textbox and set it to a variable so your send_keys() will know where to input the text. You then need to find the enter button's path and click it. Something along the lines of this should help:

elem = driver.find_element_by_xpath('textbox xpath here')
elem.send_keys('what you want to put into textbox')
driver.find_element_by_xpath('your enter button xpath here').click()

If you could provide the website or an example, it would be possible for me to give more specific code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.