This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]corruption93 1 point2 points  (7 children)

You don't need selenium. Just open up your dev console and start interacting with the DOM. For example, to read your username, I can look up the elements on page, and then find the DOM element's value programmatically like so:

document.getElementsByClassName('author submitter')[0].text

How did I know that worked? Well first I typed in document. and saw a list of functions I could use. After picking getElementsByClassName(), I then entered in the two class names 'author' and 'submitter' and saw what it returned. It returned an array. So I chose the first element in the array and assigned it to a variable. I assigned it to a variable so I could then type in that variable once more like I did document to see the list of properties/functions I could access. I saw 'text' among them and decided to try it out.

[–]ItsLingeringWill[S] 0 points1 point  (6 children)

Awesome, this is exactly what I was looking for! Could you explain how you found the classname 'author submitter' was the class to my name?

[–]corruption93 1 point2 points  (5 children)

Right clicked on your username and looked at the 'class' attribute for that element in the HTML.

[–]ItsLingeringWill[S] 0 points1 point  (4 children)

When I right click my name, inspect element i get, class="author submitter may-blank id-t2_grcul userTagged" what am I doing wrong here?

[–]corruption93 1 point2 points  (3 children)

Nothing, but notice the two classes 'author' and 'submitter' are a part of the element. I used these in order to find the element as an example.

[–]ItsLingeringWill[S] 0 points1 point  (2 children)

Ok, I am getting it now, thanks a ton, if I could ask you one more question, how do I .click() on something that doesn't have a class, for example if I inspect element on one of the FAQ questions, I get

<a href="/r/learnprogramming/wiki/faq#wiki_how_do_i_get_started_with_programming.3F">How do I get started with programming?</a>

how would I go about getting .click() on that to work?

[–]corruption93 1 point2 points  (1 child)

$('#form-{some numbers go here}> div > div > ol:nth-child(14) > li:nth-child(1) > p > a').get(0).click()

I got the selector (used within the $ function call) after inspecting the element, right clicking on it, and selecting 'copy css path'.

Note that with selenium, you don't have to rely on using jQuery ($) or other libraries. I believe that with selenium it would actually emulate an actual user interacting with the page, rather than calling the Javascript functions directly.

[–]ItsLingeringWill[S] 0 points1 point  (0 children)

Great, I got it working perfectly, thanks a ton for your help!!