Babar Ali
Babar Ali

Reputation: 71

Cannot Click a button With Selenium

I am trying to automate a process of clicking some buttons. Although I have come really far and only 1 button that is not being able to work . I am using python with selenium. So I just want to click this button but I am unable to do so. Below is my code I have tried with css select and by xpath but Still I am unable to click it , I am getting error path not found.

This is the button that I want to click

<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit" type="button" onclick=";return false;" data-channel-creation-token="GhaqucG9ARAKDi9teV92aWRlb3M_bz1VKAQ%3D"><span class="yt-uix-button-content">CREATE CHANNEL</span></button>

I have tried the following 2 codes but none of them work.

 driver.find_element_by_xpath("//button[@class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit']").click()

driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit').click()

Upvotes: 0

Views: 2250

Answers (5)

coder42
coder42

Reputation: 92

You can try this code

driver.find_element_by_css_selector('button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit').click();

Upvotes: 0

Shok9894
Shok9894

Reputation: 67

You can click on your element with next method:

driver.find_element_by_xpath("//span[(@class='yt-uix-button-content') and contains(text(), 'CREATE CHANNEL')]/..").click()

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

Assuming you want to click on the button CREATE CHANNEL you need to consider the presence of the <span> tag, within the <button> tag. You can use the following line of code:

driver.find_element_by_xpath("//button[@class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit']/span[class='yt-uix-button-content']").click()

Upvotes: 1

Satish Michael
Satish Michael

Reputation: 2015

can you try using by_class_name. You are using xpath and css to look up the class name

driver.find_element_by_class_name('yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit')

Upvotes: -2

alecxe
alecxe

Reputation: 473753

Let's go over your attempts:

  • driver.find_element_by_xpath("//button[@class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()

    This one did not work because you are trying to put a CSS selector into a @class attribute value check. You meant to do something like:

    //button[@class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary  create-channel-submit']
    
  • driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()

    This one did not work since you are not specifying multiple classes in a CSS selector correctly, classes need to be separated with a dot:

    button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit
    

Note that a much simpler selector should do the job - you don't have to specify all classes in a CSS selector - pick a more data-oriented and unique one, in this case I think this should be reasonably reliable and readable:

driver.find_element_by_css_selector('button.create-channel-submit').click()

Upvotes: 6

Related Questions