第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Selenium Python 點(diǎn)擊??只有一半的時(shí)間有效

Selenium Python 點(diǎn)擊??只有一半的時(shí)間有效

PHP
心有法竹 2023-11-09 20:18:23
但我正在嘗試為我的網(wǎng)站編寫一個(gè)單元測試,該測試會(huì)遍歷所有鏈接,并在網(wǎng)站正常工作時(shí)返回“A ok”或“no go”。但我在使用該程序時(shí)遇到了問題,它無法不斷單擊站點(diǎn)導(dǎo)航欄中的鏈接。我嘗試過多次隱式等待。明確的預(yù)期條件,但頁面加載時(shí),有一半的時(shí)間會(huì)單擊鏈接并轉(zhuǎn)到網(wǎng)站的該部分,而另一半的程序只是停止,沒有單擊任何內(nèi)容。from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver import ActionChainsPATH = "C:\Program Files (x86)\chromedriver.exe"drive = webdriver.Chrome(PATH)drive.get("https://www.blackhempfamily.com/")wait = WebDriverWait(drive, 10)link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Why Black Hemp?")))link.click()
查看完整描述

4 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

將是一個(gè)更好使用的標(biāo)簽。

wait.until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Why Black Hemp?']")))



查看完整回答
反對 回復(fù) 2023-11-09
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

嘗試使用 xpath 代替,并使用要定位的元素(不可單擊),因?yàn)樗且粋€(gè)段落。這對我有用:


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver import ActionChains


PATH = "C:\Program Files (x86)\chromedriver.exe"


drive = webdriver.Chrome(PATH)

drive.get("https://www.blackhempfamily.com/")


linkWait = EC.element_to_be_located((By.XPATH, "//div/p[contains(., 'Why Black Hemp?')]"))

WebDriverWait(drive, 10).until(linkWait)

link = drive.find_element_by_xpath("//div/p[contains(., 'Why Black Hemp?')]")

link.click()


查看完整回答
反對 回復(fù) 2023-11-09
?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

所以,花了一段時(shí)間......但是,我認(rèn)為我能夠解決這個(gè)問題。您需要執(zhí)行的操作是:

  1. 點(diǎn)擊“為什么是黑大麻?”

  2. 等待頁面停止?jié)L動(dòng)

  3. 滾動(dòng)到頁面頂部

  4. 等待頁面停止?jié)L動(dòng)

  5. **嘗試向下滾動(dòng)以便可以nav顯示該欄

  6. 重復(fù)直到您滿意/通過“A-OK”測試

為了實(shí)現(xiàn)這一點(diǎn),您需要導(dǎo)入以下內(nèi)容

from selenium import webdriver

from selenium.webdriver.chrome.webdriver import WebDriver as ChromeWebDriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait as DriverWait

from selenium.webdriver.support import expected_conditions as DriverConditions

from selenium.common.exceptions import WebDriverException

import time

第 1 步 - 點(diǎn)擊“為什么是黑大麻?” nav條形元素


chrome_driver.find_element(By.XPATH, "//nav[contains(@id, 'navContainer')]//p[text()='Why Black Hemp?']/../../..").click()

第 2 步 - 檢查頁面是否仍在滾動(dòng)


# Checks to see if our page is still scrolling

    while is_same_position == False:

        windowPosition1 = chrome_driver.execute_script("return document.body.scrollHeight;")

        time.sleep(2)

        windowPosition2 = chrome_driver.execute_script("return document.body.scrollHeight;")

        if(windowPosition1 == windowPosition2):

            is_same_position = True

            final_window_position = windowPosition1

第 3 步 - 滾動(dòng)到頁面頂部


chrome_driver.execute_script("window.scrollTo(0, {0})".format((0 - final_window_position)))

第 4 步 - 檢查頁面是否仍在滾動(dòng)


# Checks to see if our page is still scrolling

    while is_same_position == False:

        windowPosition1 = chrome_driver.execute_script("return document.body.scrollHeight;")

        time.sleep(2)

        windowPosition2 = chrome_driver.execute_script("return document.body.scrollHeight;")

        if(windowPosition1 == windowPosition2):

            is_same_position = True

第 5 步 - 嘗試向下滾動(dòng),直到我們的header標(biāo)簽沒有styleofvisibility: hidden


# Scrolls down until our nav bar is displayed

    for scrollNum in range(10):

        chrome_driver.execute_script("window.scrollTo(0, {0})".format(scrollNum * 100 + 200))

        time.sleep(2)

        if is_displayed(chrome_driver, "//header[contains(@style, 'visibility: hidden')]") == False:

            break

第 6 步 - 重復(fù)直到您滿意為止


主要代碼 - 供參考


from selenium import webdriver

from selenium.webdriver.chrome.webdriver import WebDriver as ChromeWebDriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait as DriverWait

from selenium.webdriver.support import expected_conditions as DriverConditions

from selenium.common.exceptions import WebDriverException

import time



def get_chrome_driver():

    """This sets up our Chrome Driver and returns it as an object"""

    chrome_options = webdriver.ChromeOptions() 

    chrome_options.add_argument("window-size=1500,1000")

        

    # Removes the "This is being controlled by automation" alert / notification

    chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

    path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"

    return webdriver.Chrome(executable_path = path_to_chrome,

                            options = chrome_options)



def wait_displayed(driver : ChromeWebDriver, xpath : str, int = 3):

    try:

        DriverWait(driver, int).until(

            DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))

        )

    except:

        raise WebDriverException(f'Timeout: Failed to find {xpath}')

    


def is_displayed(driver : ChromeWebDriver, xpath : str, int = 3):

    try:

        webElement = DriverWait(driver, int).until(

            DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))

        )

        return True if webElement != None else False

    except:

        return False



# Gets our chrome driver and opens our site

chrome_driver = get_chrome_driver()

chrome_driver.get("https://www.blackhempfamily.com/")



# Repeats this 5 times

for repeat in range(5):

    print("Attempt to click our link. Try #{0}".format(repeat + 1))

    

    is_same_position = False

    final_window_position = 0

    

    # Checks to see if our website's elements display

    wait_displayed(chrome_driver, "//nav[contains(@id, 'navContainer')]")

    wait_displayed(chrome_driver, "//nav[contains(@id, 'navContainer')]//p[text()='Why Black Hemp?']")

    wait_displayed(chrome_driver, "//nav[contains(@id, 'navContainer')]//p[text()='Shop Black Hemp']")


    # Clicks our "Why Black Hemp?" tab

    chrome_driver.find_element(By.XPATH, "//nav[contains(@id, 'navContainer')]//p[text()='Why Black Hemp?']/../../..").click()

    

    # Checks to see if our page is still scrolling

    while is_same_position == False:

        windowPosition1 = chrome_driver.execute_script("return document.body.scrollHeight;")

        time.sleep(2)

        windowPosition2 = chrome_driver.execute_script("return document.body.scrollHeight;")

        if(windowPosition1 == windowPosition2):

            is_same_position = True

            final_window_position = windowPosition1

    

    # Checks to see if our "Natural Moisture" text displays

    wait_displayed(chrome_driver, "(//h2//span[contains(., 'Natural Moisture')]/../..)[1]")

    

    # Scrolls back to the top of the page

    chrome_driver.execute_script("window.scrollTo(0, {0})".format((0 - final_window_position)))

    is_same_position = False

    

    # Checks to see if our page is still scrolling

    while is_same_position == False:

        windowPosition1 = chrome_driver.execute_script("return document.body.scrollHeight;")

        time.sleep(2)

        windowPosition2 = chrome_driver.execute_script("return document.body.scrollHeight;")

        if(windowPosition1 == windowPosition2):

            is_same_position = True

    

    # Scrolls down until our nav bar is displayed

    for scrollNum in range(10):

        chrome_driver.execute_script("window.scrollTo(0, {0})".format(scrollNum * 100 + 200))

        time.sleep(2)

        if is_displayed(chrome_driver, "//header[contains(@style, 'visibility: hidden')]") == False:

            break



chrome_driver.quit()

chrome_driver.stop_client()

print('Congratulations! You clicked your link multiple times!')



查看完整回答
反對 回復(fù) 2023-11-09
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您正在搜索的元素不是鏈接。這是一個(gè)段落(p)。我添加了一個(gè)sleep調(diào)用以使頁面有更多的加載時(shí)間。


試試這個(gè)代碼:


time.sleep(3)

wait = WebDriverWait(drive, 10)

#link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Why Black Hemp?")))

link = drive.find_element_by_xpath('//*[@id="idh09fqo2label"]')

link.click()


查看完整回答
反對 回復(fù) 2023-11-09
  • 4 回答
  • 0 關(guān)注
  • 217 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號