找不到元素时继续滚动直到找到,但是如果找到另一个元素,则跳过当前功能并转到下一个功能

时间:2019-02-16 10:56:10

标签: python-3.x selenium-chromedriver

我正在搜索元素,而其滚动将一直等到元素存在。

我成功进行了特定的搜索,一旦找到该元素,它将停止滚动和搜索。

只有当它最终到达页面底部时,它才会继续等待,滚动和搜索,并且永远不会停止。

我如何打破这个持续循环? (我的情况与这个问题Selenium wait for either of 2 conditions to be present十分相似)

-让我说要添加两个条件: 1)如果找不到元素,请继续滚动直到找到为止, 2)在滚动的同时,一旦发现页面的底部元素,请立即保持警惕,请跳过此功能并转到下一个功能。

我的代码如下:

# -*- 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
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import unittest, time, re, time

options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2, "profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
options.add_argument("start-maximized")
options.add_argument("incognito")
##options.add_argument("--headless")
capabilities = options.to_capabilities()

email = "somename@somemail.com"  
password = "somepass"          
userID1 = "someID"  # certain name
userID2 = "someID2" # certain name


class somesearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(options=options)
        self.driver.implicitly_wait(3)
        self.base_url = "https://www.facebook.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_some_search(self):
            driver = self.driver
            driver.get("https://www.facebook.com/") 
            driver.find_element_by_id("email").click()
            driver.find_element_by_id("email").clear()
            driver.find_element_by_id("email").send_keys(email)  
            driver.find_element_by_id("pass").clear()
            driver.find_element_by_id("pass").send_keys(password)             
            driver.find_element_by_xpath("//label[@id='loginbutton']/input").click()

            driver.get(self.base_url + userID1)

            def search_element():
                wait = WebDriverWait(driver, 10)

                find_elem = None
                scroll_from = 0
                scroll_limit = 3000
                try: 
                    self.assertTrue(self.is_element_present(By.XPATH, "(//span[(@aria-level='3')  and contains(@class, '_2iel _5kx5')])"))
                    while not find_elem:
                        driver.execute_script("window.scrollTo(%d, %d);" %(scroll_from, scroll_from+scroll_limit))
                        scroll_from += scroll_limit
                        while not EC.presence_of_element_located((By.XPATH, "(//div[(@class='_3-8x _2iek')])")):
                            try:
                                wait.until(EC.presence_of_element_located((By.XPATH, "(//a[(@aria-pressed='false')  and contains(@class, '_18vj')])[1]")))
                                element = driver.find_element_by_xpath("(//a[(@aria-pressed='false')  and contains(@class, '_18vj')])[1]")
                                driver.execute_script("arguments[0].click();", element)
                            except:
                                pass


                except AssertionError as e:
                    pass

            search_element()

            driver.get(self.base_url + userID2)

            def search_element():
                wait = WebDriverWait(driver, 10)

                find_elem = None
                scroll_from = 0
                scroll_limit = 3000
                try: 
                    self.assertTrue(self.is_element_present(By.XPATH, "(//span[(@aria-level='3')  and contains(@class, '_2iel _5kx5')])"))
                    while not find_elem:
                        driver.execute_script("window.scrollTo(%d, %d);" %(scroll_from, scroll_from+scroll_limit))
                        scroll_from += scroll_limit
                        while not EC.presence_of_element_located((By.XPATH, "(//div[(@class='_3-8x _2iek')])")):
                            try:
                                wait.until(EC.presence_of_element_located((By.XPATH, "(//a[(@aria-pressed='false')  and contains(@class, '_18vj')])[1]")))
                                element = driver.find_element_by_xpath("(//a[(@aria-pressed='false')  and contains(@class, '_18vj')])[1]")
                                driver.execute_script("arguments[0].click();", element)
                            except:
                                pass


                except AssertionError as e:
                    pass

            search_element()



    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)


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

所需的结果是: 一旦找到底部元素,请跳过当前功能并移至第二个功能。

不需要的结果: 该功能即使在元素真正到达页面底部时也可以继续运行,滚动和搜索。

请帮助我解决这个问题,我确实是一个菜鸟,没有python经验。

0 个答案:

没有答案