3

The Problem

I'm trying to change the page of a database (ReferenceUSA, requires paid or university credentials) using selenium by typing the page number in and clicking enter, but the catch is that the search box is in a div tag. send_keys() only works for input and textarea tags. Here's a picture of the page navigator, and here's the HTML:

<div class="pager">
<div class="prev button" title="Hold down to scroll through records faster." style="float: left;">«</div>
<div class="page" style="text-align: center; margin: 0px 1em; float: left; width: 40px;">1</div>
<div class="next button" title="Hold down to scroll through records faster.">»</div><span style="clear: both;"></span></div>

My Attempt

I figured out how to change the innerHTML, so I really only need to hit enter on the div tag. I thought of and tried changing the innerHTML for the next button (e.g. set current page to 1207 in next_button() function if I wanted to navigate to page 1208), but the functions are all written in a global file for the site, thousands of lines long, and they all feed off each other using the same 6 variables making it essentially unreadable. I've brought this problem to one of my CS professors to no avail.

There must be a simple solution, but I'm at a loss right now. I would greatly appreciate it if anyone could offer some guidance

7
  • Screenshot and html will help. Commented Sep 4, 2017 at 4:40
  • Also how does hitting enter on the div tag help? Commented Sep 4, 2017 at 4:47
  • I don't think sendKeys works only on input and textarea, what error are you getting when you use sendKeys on div? Commented Sep 4, 2017 at 8:12
  • @TheChetan I added a screenshot and the relevant HTML. Hitting enter switches the page number to what you type when you're actually on the page, so I figure it should work in selenium as well. Commented Sep 4, 2017 at 12:47
  • @GaurangShah It doesn't return an error, but it doesn't do anything either. Maybe I'm not identifying the problem correctly? Here's the code I tried: start_page = driver.find_element_by_xpath(XPATH) driver.execute_script('arguments[0].innerHTML = "1207";', start_page) start_page.send_keys(Keys.RETURN) Commented Sep 4, 2017 at 12:51

1 Answer 1

0

Update:

I ended up scrapping Selenium altogether and figured out how to do it using the Requests module. The following is the snippet of code for my workaround:

Select start page

data_data = {
    'requestKey': page_data,  # Collected earlier in the code
    'direction': 'Ascending', # Collected with Chrome DevTools
    'pageIndex': page_index   # Page number - 1
}
data = r.post(SELECT_PAGE, data = data_data)

Rather than clicking the button in Selenium, I figured out every time the page number changed I was sending a POST request, so I just sent the POST request.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.