簡體   English   中英

如何讓selenium一個接一個地執行兩個不同的按鈕

[英]How to make selenium execute two different buttons one after the other

我有 selenium 試圖一個接一個地執行兩個不同的按鈕,根據我嘗試的方法,我得到不同的錯誤。 所以這就是代碼的樣子

wait=WebDriverWait(driver, 10)
elem=wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[@data-componentname='gender']//span[text()='Male']/preceding::input[@type='button']")))
driver.execute_script("arguments[0].click();", elem)

我正在嘗試實施以下行

joinBtn=wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='JOIN US']"))).click()

但是,每次我嘗試時都會出現錯誤,例如

selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined

JOIN US html 具有動態 ID,因此每次刷新都不同

<div id="712eb3a9-b9a7-4cf7-8c0f-b4ae2b710e36" class="submit-button joinSubmit component blurred">

<input id="f357cd20-f2d5-4b1c-a379-8ad475a10daf" type="button" value="JOIN US">
</div>

調用click()不會返回任何內容。 因此,當您的程序嘗試將null分配給joinBtn時,您會看到錯誤:

selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined

我剛剛創建了兩個不同的函數,然后一個接一個地運行

def male():
    elem=wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[@data-componentname='gender']//span[text()='Male']/preceding::input[@type='button']")))
    driver.execute_script("arguments[0].click();", elem)


def join():
    joinBtn=wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='JOIN US']"))).click()
    driver.execute_script("arguments[0].click();", joinBtn)

似乎存在沖突,因此在每次執行之間將它們分開已經解決了這個問題。 execute_script成員希望同時單擊這兩個按鈕,因此最好將它們分開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM