user3280396
user3280396

Reputation: 19

Having trouble clicking on a button using id, xpath, class, etc

I have a button that I'm trying to click and need some help from the group. This button is found in the backend / admin area of my wordpress site (I'm trying to mass upload data into a directory listing system). I'm thinking that I need to use the button class because there are two data-id elements with the same number on the same page. I have provided my selenium code (python) attempts as well as the html I'm trying to access. Any help appreciated!!

HTML:

<div class="pkg-button">
    <a data-id="38579" class="btn btn-lg btn-primary button select-plan">Select</a>
</div>

Here's the html code snippet that has the conflicting id.

<ul data-price="0"  data-subscribed='0' data-id="38579" data-type="1"  class="packagelistitems " >

=============

Code Method 1:

elem = driver.find_element_by_id("38579").click()

Code Method 2:

driver.find_element_by_class_name('btn btn-lg btn-primary button select-plan').click()

Code Method 3:

elements = driver.find_elements_by_class_name("btn btn-lg btn-primary button select-plan")
for e in elements:
    e.click()

Code Method 4:

driver.find_element_by_xpath('//*[@id="plan"]/div[1]/ul/li/div/div/div[2]/div[2]/a').click()

For this last code snippet (#4), I'm getting the following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a data-id="38579" class="btn btn-lg btn-primary button select-plan">...</a> is not clickable at point (659, 14). Other element would receive the click: <div id="wpadminbar" class="">...</div>
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)

Upvotes: 1

Views: 1017

Answers (5)

Kiran Cherian
Kiran Cherian

Reputation: 53

You may want to look at using the find_element_by_css_selector. For the above, using your browser, right on the part of the html code of interest, click on "copy" and then "copy selector".

How to css selector.

Upvotes: 0

nilesh
nilesh

Reputation: 14307

You could try

linkText directly since its a link with Text

find_element_by_link_text("Select")

Css

div.pkg-button>a[data-id='38579']

or

div[class='pkg-button']>a[data-id='38579']

or lastly XPATH

.//div[@class='pkg-button']/a[text()='Select']

While your attempts do not work because,

  1. elem = driver.find_element_by_id("38579").click() This doesn't work since there is no element which has "id" attribute matching below, "data-id" is different than "id"

  2. driver.find_element_by_class_name('btn btn-lg btn-primary button select-plan').click() This selector is broad. There could be many elements available, and it could click on something that you may don't desire

  3. elements = driver.find_elements_by_class_name("btn btn-lg btn-primary button select-plan") for e in elements: e.click() Similar to #2 above, something could randomly fail before it could click on the element you wish for

  4. driver.find_element_by_xpath('//*[@id="plan"]/div[1]/ul/li/div/div/div[2]/div[2]/a').click() This is just way to brittle selector, I don't expect it to work. Neither you could write selenium scripts with such selectors, your automation will fail left, right and center.

Upvotes: 0

Boy
Boy

Reputation: 1197

find_element_by_id is searching for unique id attribute, not custom named attributes like data-id

try this:

driver.find_element_by_xpath("//a[@data-id='38579']").click()

Upvotes: 0

Davide Patti
Davide Patti

Reputation: 3471

Try with:

driver.find_element_by_xpath("//div[@class='pkg-button'] [a[text()='Select']]").click()

in this way you select the element with class="pkg-button"

Upvotes: 0

Vitaliy Moskalyuk
Vitaliy Moskalyuk

Reputation: 2583

It's not clear why element is not clickable (probably you should click on div wrapper and not an a element), so try these, something should work)

to click on div:

//div[@class='pkg-button'][a[text()='Select']]

to click on a tag

//div[@class='pkg-button']/a[text()='Select']

//a[text()='Select']

Important: Please note that in method4 you received error where element with id wpadminbar overlapped your item, so check this in browser dev tools to be sure that some div is not overlapping element you want to click =)

Upvotes: 2

Related Questions