繁体   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