首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Selenium rel=nofollow链接

Python Selenium rel=nofollow链接
EN

Stack Overflow用户
提问于 2019-03-30 06:44:06
回答 1查看 225关注 0票数 0

Python Selenium代码

代码语言:javascript
复制
driver.find_element_by_link_text("Off").click()

是正确的,并且执行时没有错误。错误是点击跟随着"nofollow“到404错误。此代码应该只更改网页的视图状态。这种行为对于Firefox和Chrome webdriver都是正确的。对我的开发环境唯一的其他更改是Linux内核更新。

如果有任何建议,我将不胜感激。

下面是完整的示例:

代码语言:javascript
复制
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class UntitledTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.katalon.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_untitled_test_case(self):
        driver = self.driver
        driver.get("https://www.modelmayhem.com/portfolio/768765/viewall")
        driver.find_element_by_link_text("Off").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
        else:
            alert.dismiss()
        return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

    def links_processor(self, links):

        # A hook into the links processing from an existing page, done in order to not follow "nofollow" links
        ret_links = list()
        if links:
            for link in links:
                if not link.nofollow: ret_links.append(link)
        return ret_links

if __name__ == "__main__":
    unittest.main()
EN

回答 1

Stack Overflow用户

发布于 2019-03-30 07:03:40

问题是cookies弹出。使用implicitly_wait只需在“我同意”按钮上添加点击即可:

代码语言:javascript
复制
driver.find_element_by_link_text("I AGREE").click()
driver.find_element_by_link_text("Off").click()

如果您将使用WebDriverWait,以等待“我同意”将被单击并关闭它:

代码语言:javascript
复制
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "I AGREE"))).click()
driver.find_element_by_link_text("Off").click()

cookies弹出屏幕截图:

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55426341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档