Reputation: 65
url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292"
This webpage has pagination for comments in the end. When I inspected the page, the buttons were compiled like below:
<a href="#" class="u_cbox_page" data-action="page#move" data-log="RPC.pgnum"><span class="u_cbox_num_page">3</span></a>
I want to click one of these buttons, so I tried
driver = webdriver.Chrome(driver_path)
driver.get(url_main)
driver.switch_to.frame('commentIframe')
view_all_comments = driver.find_element_by_xpath('''//*[@id="cbox_module"]/div/div[8]/a''')
view_all_comments.click()
page_buttons = driver.find_elements_by_css_selector(".u_cbox_page")
page_buttons[2].click()
But it returns
StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=87.0.4280.88)
How can I fix this?
Upvotes: 1
Views: 87
Reputation: 573
Wait for the first button to be clickable using WebDriverWait
element_to_be_clickable
and wait for the presence of the other buttons using WebDriverWait
visibility_of_all_elements_located
like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
driver = webdriver.Chrome(driver_path)
driver.get(url_main)
driver.switch_to.frame('commentIframe')
wait = WebDriverWait(driver, 10)
view_all_comments = wait.until(ec.element_to_be_clickable((By.XPATH, '''//*[@id="cbox_module"]/div/div[8]/a''')))
view_all_comments.click()
page_buttons = wait.until(ec.visibility_of_all_elements_located((By.CSS_SELECTOR,".u_cbox_page")))
page_buttons[2].click()
Upvotes: 1