[英]How to make selenium execute two different buttons one after the other
I have selenium trying to execute two different buttons one after the other and I'm getting different errors depending on the method I try.我有 selenium 试图一个接一个地执行两个不同的按钮,根据我尝试的方法,我得到不同的错误。 So this is what the code looks like
所以这就是代码的样子
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)
And I am try to implement the following line我正在尝试实施以下行
joinBtn=wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='JOIN US']"))).click()
However every time I try I get an error such as但是,每次我尝试时都会出现错误,例如
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined
The JOIN US html has a dynamic ID so every refresh it's different 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>
Invoking click()
doesn't returns anything.调用
click()
不会返回任何内容。 Hence while your program tries to assign null
to joinBtn you see the error:因此,当您的程序尝试将
null
分配给joinBtn时,您会看到错误:
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of undefined
I Just created two different functions and ran 1 after the other我刚刚创建了两个不同的函数,然后一个接一个地运行
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)
There seemed to be conflict so separating them between each execution has solved the issue.似乎存在冲突,因此在每次执行之间将它们分开已经解决了这个问题。 The
execute_script
member wants to click both buttons so keeping them separate works best. execute_script
成员希望同时单击这两个按钮,因此最好将它们分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.