-1

I am using Xpath helper in chrome extension to find xpath for python programming. I have read the documentation but is there a better guide out there.

HTML is as follows:

<form class="addComment expand" data-id="9644797">
    <img src="https://ctl.s6img.com/society6/img/g2taHIrokQ01R_67jS8ulaWI2wk/h_150,w_150/users/avatar/~artwork/s6-original-art-uploads/society6/uploads/u/sul97/avatar_asset/d837ee10016843a3bba9ae3310cc338d" width="25" height="25">
    <textarea placeholder="Add a comment..." data-button="9644797"></textarea>
    <button id="b9644797">Comment</button>
</form>

Can someone show me an example to click the button of this path? I have tried to use it but didnt work

submit_comment = driver.find_element_by_xpath("/html[@class='gr__society6_com']/body[@class='platform- desktop']/div[@id='wrap']/div[@id='content']/div[@id='MySociety']/div[@class='one-col clearfix']/ul[@id='Mason']/li[@id='9649102']/div[@class='item-wrap']/div[@class='comments']/form[@class='addComment expand']/button[@id='b9649102']").click()
6
  • "html example"... No it's not an HTML example... Share HTML, not XPath Commented Sep 6, 2018 at 17:13
  • added html example here Commented Sep 6, 2018 at 17:22
  • Why do you need a complicated xpath? Why not just use the id of the button? //button[@id='b9644797'] Commented Sep 6, 2018 at 17:24
  • @BryanOakley : because the id is random generated. So we can’t rely on this id. Commented Sep 6, 2018 at 17:26
  • Your <img... tag is not closed, so your markup is not well-formed. Maybe that's a cause for misbehaviour. Commented Sep 6, 2018 at 18:21

1 Answer 1

4

The xpath would be :

//button[text()='Comment']  

However, you should introduce webdriver wait for more stability in your code.

.click() method return void. So invoking click method and storing in a variable doesn’t make any sense.

Sign up to request clarification or add additional context in comments.

2 Comments

also is webdriver wait is same as time.sleep() ?
time.sleep() is extreme case of webdriverwait. You can refer this link selenium-python.readthedocs.io/waits.html for more details.

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.