简体   繁体   中英

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. 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

<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. Hence while your program tries to assign null to joinBtn you see the error:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM