Python Selenium rel = nofollow link - PullRequest
       54

Python Selenium rel = nofollow link

0 голосов
/ 30 марта 2019

Код Python Selenium

driver.find_element_by_link_text("Off").click()

правильный и выполняется без ошибок.Ошибка в том, что щелчок следует за «nofollow» до ошибки 404.Этот код должен только изменить состояние просмотра веб-страницы.Такое поведение верно как для Firefox, так и для Chrome webdriver.Единственными другими изменениями в моей среде разработки были обновления ядра Linux.

Буду признателен за любые предложения.

ПОЛНЫЙ ОБРАЗЕЦ НИЖЕ:

# -*- 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()

1 Ответ

0 голосов
/ 30 марта 2019

Проблема во всплывающем окне с файлами cookie.С помощью implicitly_wait просто добавьте щелчок к кнопке «Я СОГЛАСЕН»:

driver.find_element_by_link_text("I AGREE").click()
driver.find_element_by_link_text("Off").click()

Если вы будете использовать WebDriverWait, подождите, пока «Я СОГЛАСЕН», и нажмите кнопку «Закрыть»:

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()

Скриншот всплывающего окна с файлами cookie:
enter image description here

...