【问题标题】:How do I close live chat window without cross using selenium?如何使用 selenium 在不交叉的情况下关闭实时聊天窗口?
【发布时间】:2021-10-21 06:00:16
【问题描述】:

我正在尝试从该页面https://everdeskplus.com/products/everdesk-max 上抓取评论,但我遇到了这个错误。我需要它单击“>”按钮,直到它到达最后一页。它不允许我点击'>'按钮并说找不到。

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)

driver.get("https://everdeskplus.com/products/everdesk-max")

wait = WebDriverWait(driver,30)

# Close Spin pop-up
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@id='pge_HN8znV-LA']/div[1]/span"))).click()


# Switch to Iframe and close the chat box
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"chat-button")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@id='gorgias-chat-messenger-button']"))).click()

#Switch back to the default content
driver.switch_to.default_content()

# Scroll to the footer and click on "Next" page button 
footer = driver.find_element_by_class_name("stamped-pagination stamped-reviews-ul")
driver.execute_script("arguments[0].scrollIntoView(true);",footer)
nextbutton = wait.until(EC.element_to_be_clickable((By.XPATH,"'//*[@id='stamped-reviews-tab']/ul/li[14]/a'")))
nextbutton.click()

这是我得到的错误:


---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-4-de1d77662195> in <module>
     21 
     22 # Scroll to the footer and click on "Next" page button
---> 23 footer = driver.find_element_by_class_name("stamped-pagination stamped-reviews-ul")
     24 driver.execute_script("arguments[0].scrollIntoView(true);",footer)
     25 nextbutton = wait.until(EC.element_to_be_clickable((By.XPATH,"'//*[@id='stamped-reviews-tab']/ul/li[14]/a'")))

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_class_name(self, name)
    562             element = driver.find_element_by_class_name('foo')
    563         """
--> 564         return self.find_element(by=By.CLASS_NAME, value=name)
    565 
    566     def find_elements_by_class_name(self, name):

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    974                 by = By.CSS_SELECTOR
    975                 value = '[name="%s"]' % value
--> 976         return self.execute(Command.FIND_ELEMENT, {
    977             'using': by,
    978             'value': value})['value']

~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".stamped-pagination stamped-reviews-ul"}
  (Session info: chrome=95.0.4638.54)

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    尝试如下并确认:

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
    driver.maximize_window()
    driver.implicitly_wait(10)
    
    driver.get("https://everdeskplus.com/products/everdesk-max")
    
    wait = WebDriverWait(driver,30)
    
    # Close Spin pop-up
    wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@id='pge_HN8znV-LA']/div[1]/span"))).click()
    
    # Switch to Iframe and close the chat box
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"chat-button")))
    wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@id='gorgias-chat-messenger-button']"))).click()
    
    #Switch back to the default content
    driver.switch_to.default_content()
    
    # Scroll to the footer and click on "Next"
    footer = driver.find_element_by_id("vpc-configure-footer")
    driver.execute_script("arguments[0].scrollIntoView(true);",footer)
    nextbutton = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='vpc-sticky-next-btn']")))
    nextbutton.click()
    

    更新: 替换以下代码:

    # Scroll to the footer and click on "Next" page button 
    footer = driver.find_element_by_class_name("stamped-pagination stamped-reviews-ul")
    driver.execute_script("arguments[0].scrollIntoView(true);",footer)
    nextbutton = wait.until(EC.element_to_be_clickable((By.XPATH,"'//*[@id='stamped-reviews-tab']/ul/li[14]/a'")))
    nextbutton.click()
    

    有了这个

    # Scroll to the footer and click on "Next" page button
    nextbutton = wait.until(EC.element_to_be_clickable((By.XPATH,"//li[@class='next']/a")))
    driver.execute_script("arguments[0].click();",nextbutton)
    

    【讨论】:

    • 感谢您的回复,但我收到此错误 ElementClickInterceptedException: Message: element click intercepted: Element
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 2022-08-09
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2017-03-04
    • 1970-01-01
    相关资源
    最近更新 更多