使用WebDriver中的click操作无法关闭天猫弹出的登陆界面

1.老师留的作业是用scrapy爬动态网页天猫商品的价格,但是用Chrome每次点开网页的时候都会弹出登录界面,虽然不影响爬取价格,但是想把这个页面关闭 网页:https://detail.tmall.com/item.htm?id=555358967936

2.代码:

def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.
        driver = spider.drive
        driver.get(request.url)
        # driver.switch_to.frame("sufei-dialog-content")
        #因为网页需要时间渲染,在这里确定目标元素
        locator = (By.XPATH, '//span[@class="tm-price"]')
        close_btn = (By.XPATH,'//div[@class="sufei-dialog-content"]/div[@id="sufei-dialog-close"]')
        # driver.switch_to.frame("sufei-dialog-content")
        WebDriverWait(driver, 3,1).until(EC.presence_of_element_located(close_btn))
        # driver.switch_to.frame("sufei-dialog-content")
        click = driver.find_element_by_xpath('//div[@class="sufei-dialog-close"]')
        actionchain = action_chains.ActionChains(driver)
        actionchain.click(click)
        actionchain.perform()
        print('点击已结束')
        driver.switch_to.default_content()
        # driver.switch_to.parent_frame()
        #等待网页渲染,最多等待15s,并且每1s查看一次是否出现目标元素
        WebDriverWait(driver, 15, 1).until(EC.presence_of_element_located(locator))
        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        #返回请求网页后得到的源代码
        return HtmlResponse(url=request.url,body=driver.page_source,request=request,encoding='utf-8',status=200)

_3.我尝试过分析可能是iframe的问题,但是尝试过后总是提醒
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="sufei-dialog-close"]"}
(Session info: chrome=75.0.3770.80)

图片说明
蓝色的就是想要关闭的标签
感谢帮助(●'◡'●)

1个回答

orange1897
orange1897 用cookie的确可以解决登陆界面出现的问题,但是我不想登录淘宝账号,只是想单纯的关闭这个登陆界面,发现自己能力不行,关不掉,先谢过你(●'◡'●)
8 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问