Finding the xpath of a button, using it in python and selenium - python

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.

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")

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']..."

Related

Can't find twitter DM textbox by xpath using selenium

I am trying to find the textbox element using the find_element_by_xpath() method, but It keeps telling me it cant find said element, here's the line of code that does that.
I've tried finding it by link_text, partial link text, selector and it just doesn't work
bar = nav.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div/aside/div[2]/div[2]/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
Thanks in advance!
So, I suggest creating your xpath if you want to be precise and avoid taking it based on html structure (which can change).
The locators looks like:
And you can take it with xpath:
//input[#placeholder='Search people' and #role='combobox']
To avoid this problem, I suggest going trough a tutorial for a better understanding regarding how to create custom locators: Xpath tutorial
What was happening is: When I opened the tab to inspect the element, the DM structure changed because of my screen size so the xpath wasn't the same

Selenium unable to automate click on web element

I should start by saying that I'm not a web developer but I have been using Selenium with Python for a few weeks and think I've gotten the basics down.
What I have is a page with a weekly calendar on it. Image as follows:
What happens is that you can click on any of the coloured boxes which will bring up a register for a class. It features items that you can click on which bring up new information. The problem I have is that I can't click on the items - or, rather, I don't know how to automate the click. I have automated clicking with Selenium before in other situations.
That looks as follows:
As you can see, the calendar still appears in the background. That is to say, the item we click on doesn't take us to a new page but, I suppose, runs some kind of method which shows the register and populates it with data.
My problem is this: I want to automate the process so that I can click on each of these items then scrape the information. In order to do that I need to be able to automate the clicking for each of the items.
So what have I done? When I've done this before, I've searched through the web html for the relevant part and then grabbed the xPath to the element I needed. But here I can't do that. Why not? Well, firstly, I just can't find that element!
Take a look at this close up of the first column:
It's divided into columns, but then I'd expect the clickable area to be an element within that. As you can see, it's not. Furthermore, the clickable area is just the coloured box itself, but if you look closely you can see the element goes outside of that area. I have gone very close with my mouse cursor to see exactly what's clickable, and it definitely is just the coloured box.
So I've not been able to get the element at all.
I thought I might be able to just find out where we went after I clicked the button, but when I got the link address, it just said it was the same page with no differences.
I appreciate I'm asking quite a broad question here, but the problem is that I don't really know where to start. If someone could give me at least that much, I would be grateful. Like if I could just click on each of these one at a time... I've found where the populated data is so I could grab that without a problem.
Well, here's to hoping.
Edit: I should add that there are some JavaScript items (tag type script, type='text/javascript'). I presume that the answer is in there somewhere, but there is a lot of Javascript and I'm not adept at reading it. It's hard for me to tell what script does what. If I could at least figure out what script runs when I click the item then I think I'd be onto something, but I have no idea. Even that would help me.
I had encountered similar problem when scraping for Instagram followers in mobile view where it was floating box when showing the accounts followers name. The approach I took was identifying the name of floating dialog box and clicking the element in it. It might different in your case of html.
Trying looking at this link. Selenium Scroll inside of popup div
Hard to say without the HTML. Maybe try Katalon Recorder (chrome extension) and see if that can detect the xpath for you? It might also be you have to use some kind of javascript to invoke the method for the element

Selenium Python Help Getting element of a form

Iv been trying to get the form elements on this website and for some reason I cant get them. Iv tried every way I can think of. I would really appreciate some help.
Here is my latest attempt
inputs = driver.find_elements_by_xpath("//input[#type='text']")
Here is the website.
Thanks in advance.
Edit : Github link for project
Edit : Ok so I found out that if you go straigt to website that the btn points to then it works. So I am assuming that slenium does not automaticly update to the HTML of the current page. How does one do this?
if you are getting 'NoSuchElementException', try using conditional wait before you do find_element. When you are loading another page wait until the element is visible and can be interacted.
Guide for that is here
check if the element is inside in iform, if so use
driver.switchTo().frame("framename")
EDIT
Focus on something unique in the tag. For your example use the class name used in the div. Do not use the ID tag identifier as it changes over time.
//*[contains(#class, 'ginput_container ginput_container_text')]/following::*[contains(text(),'Email')]/following::div[1]/descendant::input

Xpath issues with content egg plugin

I don't usually post on here as you all seem pretty advanced for me, but this feels like an advanced question, so thought I'd ask.
I'm using the Content Egg plugin for WordPress (please don't roll eyes) and it's asking me for XPath for a price I'm trying to get. I have tried so many different xpaths and none of them seem to be working. I'm trying to get the price Xpath from this webpage. Any ideas?
https://www.hockeydirect.com/Catalogue/Hockey-Sticks/Young-Ones-Hockey-Sticks/Young-Ones-ABD-Hockey-Sticks/Young-Ones-ABD-70-Hockey-Stick-341111
This is what i've tried:
//html/body/form/div[5]/div[2]/div[3]/div/span/div[1]/div[4]/div[2]/div[2]/div/div[1]/div/span/fieldset/div[1]/div/div[1]
//*div[#class="MainPriceContainer"]
//div[#class='MainPriceContainer vcenter']
Any help would be greatly appreciated.
try to copy the xpath from firefox by this steps
inspect elements and select the price with the selector pointer it will highlight the element in the inspector html
then right click to the highlighted code then copy then select XPATH from inside copy
then paste it to the Xpath
then set the price update in the offer module setting to 30 seconds and test
if worked tell me :)

Using selenium to scrape email addresses with xpath

I want to scrape the email address from the following web page.
Facebook Business Info Page
So I decided to use the selenium driver with Python. I figured the best way to do this was through defining the xpath. From inspection of the elements, I noticed that the info I was looking for was found in the following HTML structure as seen here:
Now I must admit that I am bit of a noob when it comes to using Selenium and defining elements by xpath, so I was hoping someone would correct me if I am defining the following xpath incorrectly. This is what I have right now:
But I'm fairly certain I'm defining the wrong xpath. I know I want to grab the information in the _50f4 div class but I don't know how to define it. If someone could help me figure that out I would greatly appreciate it.
you can get the text of the email address using an xpath like : //div[#id = 'u_0_u']//ul/li[4]//div[#class = '_50f4']

Categories

Resources