简体   繁体   English

如何让selenium一个接一个地执行两个不同的按钮

[英]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.

相关问题 如何使spawnSync和fs.readFile依次执行? - How to make spawnSync and fs.readFile to execute one after other? 如何一个接一个地执行许多功能 - How to execute many functions one after the other 如何以相同的形式利用两个不同动作的两个按钮? - How to make use of two buttons with two different actions in the same form? 如何在下面的代码中一个接一个地突出显示单选按钮 - how to highlight the radio buttons one after the other in following code 如何让一个绑定键执行tkinter、python中的两条命令? - How to make one binded key execute two commands in tkinter, python? Python Selenium 如何在一个代码中与两种不同的网站格式进行交互 - Python Selenium how to interact with two different website formats in one code 我只想在 kivy 中一个接一个地单击两个按钮时打印文本 - I want to print a text only if the two buttons are clicked one after the other in kivy 如何使两个带有 TKinter 的测试按钮运行两个相互独立的功能? Python3 - How can I make two test buttons with TKinter run two functions independent of each other? Python3 如何在 django 中按 HTML 页面上的两个不同按钮从一个视图运行不同的操作 - How can I run different actions from one view in django pushing two different buttons at HTML page 如何在 manimce 中一个接一个地应用两个转换? - How to apply two tranformations one after the other in manimce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM