1

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)

5 Answers5

2

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 =)

Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15
  • I used all 3 of your strings using "find_element_by_xpath", ".click()". Errors are: div string: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/div[@class='pkg-button'][a[text()='Select']]"} a tag #1: selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (627, 14). Other element would receive the click:
    ...
    – user3280396 Oct 20 '17 at 17:29
  • a tag #2: selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (627, 14). Other element would receive the click:
    ...
    – user3280396 Oct 20 '17 at 17:29
0

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"

Davide Patti
  • 3,391
  • 2
  • 18
  • 20
  • On the click event line of your code, I'm getting the following error: selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (627, 14). Other element would receive the click:
    ...
    (Session info: chrome=61.0.3163.100) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)
    – user3280396 Oct 20 '17 at 15:47
  • 1
    bad idea to click pkg-button without additional specification, as it could be not unique – Vitaliy Moskalyuk Oct 20 '17 at 15:52
  • @VitaliyMoskalyuk you are right. I re-edited the answer – Davide Patti Oct 20 '17 at 16:13
  • There is more than one Select button on the page (ie., they pertain to product packages for the listing). I use the MRI xpath highlight tool (bookmarklet for IE) and it using the following string highlights all the Select buttons: //div[@class='pkg-button'] – user3280396 Oct 20 '17 at 16:15
  • Retried your edit. Getting: selenium.common.exceptions.WebDriverException: Message: unknown error: Element
    ...
    is not clickable at point (627, 14). Other element would receive the click:
    ...
    – user3280396 Oct 20 '17 at 17:20
  • @user3280396 could you share your entire html? – Davide Patti Oct 20 '17 at 17:22
  • I can share the html file. However, it has more characters than what the edit field will allow & I cannot attach files in the stackoverflow.com thread. I can ftp something up and share the link. Is there another way to share files on this forum? – user3280396 Oct 20 '17 at 17:42
0

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()

Boy
  • 1,182
  • 2
  • 11
  • 28
  • Getting the following: selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (627, 14). Other element would receive the click:
    ...
    – user3280396 Oct 20 '17 at 17:18
  • refer to this: https://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error – Boy Oct 20 '17 at 22:35
0

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.

nilesh
  • 14,131
  • 7
  • 65
  • 79
0

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.

  • I was able to solve the problem with the code noted below. My issue was that I was interacting with an object at the bottom of the page, but the button I'm trying to click was midway on the page. Seems that the object I want to interact with has to be physically viewable at the time of script interaction. Is this true? I would think that all objects are loaded into memory and could be accessed randomly. Someone please comment on this. driver.find_element_by_xpath('//*[@id="plan"]/div[1]/ul/li/div/div/div[2]/div[2]/a').click() – user3280396 Oct 22 '17 at 00:11
  • Yes. In selenium the element has to be visible and intractable else you will be unable to complete the action. This is more like simulating a user action. – Kiran Cherian Oct 22 '17 at 00:36