2

I am unsure if any of you are familiar with Reddit, however I want to start a small subreddit for some warhammer lore questions, where people can post questions and then answer them. To highlight the questions that are answered I want a moderator account to automatically upvote them once they are "Solved", which I am trying to do with Selenium, however I am running into some troubles finding the upvote button.

Currently, I am able to log in with my moderator account, however I am unable to press the upvote button, I have tried the following code to no avail:

driver.get("https://www.reddit.com/r/ChosenSub/ChosenThread")

time.sleep(3)

driver.find_element_by_xpath("div[@id='siteTable']/div[@id='thing_t3_XXXXXX']/div[@class='midcol unvoted']/div[@class='arrow up login-required access-required']").click

Where the XXXXX is an id of the thread in question, however this produces absolutely no result. I am fairly familiar with Python, but in no way xPath, I used the XPath helper tool in Chrome to get the XPath above, but still no luck

If anyone has any potential ideas please do let me know, any and all help is very appreciated.

1
  • 1
    Please provide your page html snippet so we could help you with xpath. Commented Jan 17, 2017 at 15:48

2 Answers 2

1

Considering provided in comments link, you can try to use simplified XPath as below:

driver.find_element_by_xpath("//div[@id='thing_t3_XXXXXX']//div[@aria-label='upvote']").click()

If you need more common method to upvote question by its id (if id value is predefined):

def upvote_question(question_id):
    driver.find_element_by_xpath("//div[@id='%s']//div[@aria-label='upvote']" % question_id).click()

And then you can just use it with a question's id as argument:

upvote_question("thing_t1_dcjl4vu")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, however, when I try to fire it off I get the following error: Message: Unable to locate element: {"method":"xpath","selector":"//div[@id='thing_t3_5oiv2p']//div[@aria-label='upvote']"}
My mistake, I apparently have the language set to local which is why it didnt work. Thank you so much for your help, I really appreciate it!
1

You probably need to add '//' in front of that xpath so that it finds the div anywhere in the document, otherwise it would have to be at the root of the html (which it most likely is not). So the xPath would be:

"//div[@id='siteTable']..."

1 Comment

Thanks for your help! It seems like it is able to find something as it does not return an error, however it does not seem like it actually ends up clicking the button itself.

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.