Reputation: 711
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.
Upvotes: 2
Views: 870
Reputation: 52665
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")
Upvotes: 1
Reputation: 1764
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']..."
Upvotes: 1