pythonselenium-webdriverseleniumbase

SeleniumBase click on button via text within it using XPATH


I am currently writing a program in Python using SeleniumBase, however, I want to click a button via the text inside it and it is not working. This is an example code block:

from seleniumbase import BaseCase

BaseCase.main(__name__, __file__)

class MultipleDriversTest(BaseCase):
    def test_multiple_drivers(self):
        
        LINK = "https://invideo.io/perf/ga-ai-video-generator-web/?utm_source=google&utm_medium=cpc&utm_campaign=Top16_Search_Brand_Exact_EN&adset_name=InVideo&keyword=invideo&network=g&device=c&utm_term=invideo&utm_content=InVideo&matchtype=e&placement=g&campaign_id=18035330768&adset_id=140632017072&ad_id=616240030555&gad_source=1&gclid=Cj0KCQiAvvO7BhC-ARIsAGFyToWFf0L_8iqkB32qg9prKxVApsklZ8HA69LW2O0Z6XC1nbXXz9sCTTEaAinZEALw_wcB""https://invideo.io/perf/ga-ai-video-generator-web/?utm_source=google&utm_medium=cpc&utm_campaign=Top16_Search_Brand_Exact_EN&adset_name=InVideo&keyword=invideo&network=g&device=c&utm_term=invideo&utm_content=InVideo&matchtype=e&placement=g&campaign_id=18035330768&adset_id=140632017072&ad_id=616240030555&gad_source=1&gclid=Cj0KCQiAvvO7BhC-ARIsAGFyToWFf0L_8iqkB32qg9prKxVApsklZ8HA69LW2O0Z6XC1nbXXz9sCTTEaAinZEALw_wcB"
        self.activate_cdp_mode(LINK)
        driver = self.driver
        driver.sleep(10)
        driver.click('//button[contains(text(), "Sign up"]')

When I run this, I get the error:

Element {//button[contains(text(), "Sign up"]} was not found after 7 seconds!

I believe the XPATH to be correct, or have I done something wrong?


Solution

  • You have missed the closing parenthesis for contains function. That is why it is not finding the element. The following is working.

     driver.click('//button[contains(text(), "Sign up")]')