So, I right now have a working piece of code that looks like this;
if browser.find_elements_by_xpath("//div[contains(@class, 'dealspg_item_cell')]"):
snipes = browser.find_elements_by_xpath("//div[contains(@class, 'dealspg_item_cell')]")
else:
print "Cant find snipes.. Retrying..."
browser.get("https://www.rolimons.com/deals")
time.sleep(2)
print "Reloaded browser... Retrying..."
if browser.find_elements_by_xpath("//div[contains(@class, 'dealspg_item_cell')]"):
snipes = browser.find_elements_by_xpath("//div[contains(@class, 'dealspg_item_cell')]")
else:
print "Shutting down engine..."
browser.quit()
checking = False
print "Restarting script..."
break
This all works fine. And it gets 60 elements. Now I only need the first 10. How can I make it that it will only find_elements_by_xpath
with a limit of 10?
Is there any way to do this?
Thanks!
==== EDIT ====
This code takes at the moment 4 seconds. And I hope to reduce this time by putting the max on 10.
you can use:
//[yourXpath]/*[position()<=10]
in Your case
//div[contains(@class, 'dealspg_item_cell')]/*[position()<=10]
Although using xpath is good answer. You can also refer css selector as per performance respective. Personally i faced such issue where xpath is not working but CSS selector is
div.dealspg_item_cell:nth-child(-1n+10)
Explanation nth-child(-1n+10)
+10
indicates here the node position, from where we want to pick the elements -1n
all nodes before the 10th node
Lets say there are total 11 element then selection would be something like :
Another example would be nth-child(1n+10)
, It means all other nodes after 10th node
You can explore more from here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.