1

I've been struggling with this selenium project and keep finding old references or posts that marginally relate to this issue. I admit I am new to xpath and selenium so hopefully it may be as simple as fixing my syntax. I'm using python 3.6.9 on lubuntu 19.10 if that makes any difference.

html element I'm trying to access:

 <div class="NfvXc">
   <textarea aria-label="Write a caption…" autocomplete="off" autocorrect="off"
   class="_472V_" placeholder="Write a caption…"></textarea>
  </div>

Code I've tried:

add_text = browser_object.find_element_by_xpath("//textarea[contains(@aria-label,'Write a caption...')]")

add_text = browser_object.find_element_by_xpath("//div[contains(text(),'Write a caption...')]")

Error messages I am ending up with when trying different xpaths:

error in process_image(): Message: Given xpath expression "//*div[contains(text(),'Write a caption...')]" is invalid: SyntaxError: The expression is not a legal expression.

I checked firefox to try and trap the xpath, shows up as: /html/body/div[1]/section/div[2]/section[1]/div[1]/textarea

Although I'm not sure how to adjust my xpath to find that element as of yet, obviously.

eddyizm
  • 327
  • 2
  • 9
  • 2
    You are using `//*div[contains(@placeholder,'Write a caption...')]`, use `//div[contains(@placeholder,'Write a caption...')]` or use this if you are sure that there are not other elements in the page containing placeholder `Write a caption...` `//*[contains(@placeholder,'Write a caption...')]` – CodeIt Feb 23 '20 at 06:07

1 Answers1

1

The XPath from the error message //*div[contains(text(),'Write a caption...')] is invalid, because you cannot have both * and div and the beginning of the XPath. Choose one of them, for example //div[contains(text(),'Write a caption...')].
However it will still not work as the text is in an attribute, you can use //textarea[@aria-label='Write a caption…']
Note that //textarea[contains(text(),'Write a caption…')] will return no match. You can test your XPaths in the browser using the syntax $x() in the console, more info here.

K. B.
  • 1,356
  • 2
  • 14
  • 20
  • Thank you for the clarification and answer! I also want to note the thing that was not clear to me was that the the three periods were actually an ellipses which obviously was not matching. – eddyizm Feb 23 '20 at 17:22