python - Selenium, Get Elements By Xpath - Only grab last 60 elements on page -


i'm having little trouble working out how can specify last 60 elements on page

posts = driver.find_elements_by_xpath("""(//div[@class='hotproductdetails'])""") post in posts:     print(post.text) 

this code prints every bit of text within elements on webpage. i'm trying scrape site has 'load more' button on it.

the 'load more' button loads 60 more products, , i'd code grab products. way can stick in loop clicks button, grabs products loads, append pandas dataframe , repeats set number of iterations.

i've been unable code me, , once load more button pressed lot of times, grabbing elements can kill chrome , in turn script.

"(//div[@class='hotproductdetails'])[position() > {} , position() <= {}])".format ((page -1 ) * 50, page * 50) 

someone shared code me, crashes me error:

invalid selector: unable locate element xpath expression (//div[@class='hotproductdetails'])[position() > {} , position() <= {}])".format ((page -1 ) * 50, page * 50 because of following error: syntaxerror: failed execute 'evaluate' on 'document': string '(//div[@class='hotproductdetails'])[position() > {} , position() <= {}])".format ((page -1 ) * 50, page * 50' not valid xpath expression.   (session info: chrome=60.0.3112.90)   (driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=windows nt 10.0.14393 x86_64) 

this first time i've ever had web-scraping project , used selenium (which amazing package, impressed it) , i'm not sure fix it. suspect 'page' code, sits on same webpage gets larger load more products.

i can share website i'm scraping if helps - said first scraping project , company joined. don't know if upset me sharing.

if getting invalid xpath selector wrong. there ")" @ end. below works me

page = 2  xpath_selector = "(//div[@class='hotproductdetails'])[position() > {} , position() <= {}]".format ((page -1 ) * 50, page * 50) 

also if want last 60 elements can use below

xpath_selector = "(//div[@class='hotproductdetails'])[position() > last() - 60]" 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -