My task involves utilizing Python to extract data from a website that features a filter pane requiring scrolling. I came across a code snippet that aids in navigating through a list of elements by iterating through a loop.
recentList = driver.find_elements_by_xpath("/html/body/div[3]/main/div/div/div/div[1]/div/form/div[2]/fieldset[3]/div/ul/li")
for item in recentList:
driver.execute_script('arguments[0].scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"})', item)
I already have a for loop integrated into my existing code and now I aim to incorporate an element that requires scrolling as well. Following the logic presented above (simplified loop):
for p in range(1,15):
item = driver.find_element_by_xpath(str('/html/body/div[3]/main/div/div/div/div[1]/div/form/div[2]/fieldset[3]/div/ul/li[[' + str(p) + ']'))
driver.execute_script('arguments[0].scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"})', item)
I'm encountering difficulties as the code doesn't seem to work as intended. The error message I received is as follows:
selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression "/html/body/div[3]/main/div/div/div/div[1]/div/form/div[2]/fieldset[3]/div/ul/li[[1]" is invalid: SyntaxError: The expression is not a legal expression.
If anyone knows how to resolve this issue, your input would be greatly appreciated. The XPath used in the last piece of code is confirmed to be accurate and functional.
I attempted replacing my current loop with "item in recentList" but encountered issues when trying to scroll through pages on the filter.