Difficulty trying to select button with id or class

39 views
Skip to first unread message

sct...@gmail.com

unread,
Oct 19, 2017, 12:18:31 PM10/19/17
to webdriver
I have a button that I'm trying to click and need some help from the group. 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 code 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()

darrell grainger

unread,
Oct 20, 2017, 9:48:06 AM10/20/17
to webdriver
The easiest way to find a locator is using the console on your browser. I tend to use Chrome. I'll open the Developer Tools. Easiest way is to right click on the web page and select Inspect. Once the Developer Tools are open you can press the ESC to toggle the console open and closed. At the console prompt I'll type something like:

document.querySelectorAll("[data-id='38579']")

where "[data-id='38579']" is a CSS selector. If I enter this and it finds one and only one result, it is a good locator for Selenium. If it finds more than one result then it isn't unique enough to be a good locator. In your case it will return two results. The first will be the <A> element and the second will be the <UL> element. In CSS this is really easy to say I want JUST the <A> element. In the console I would try the following:

document.querySelectorAll("a[data-id='38579']")
 
Adding the 'a' to the front of the CSS selector says I want the <A> tag with the attribute data-id='38579'. If this returns one and only one result you can use it as a Selenium locator. In Selenium (python) this would be:

driver.find_element_by_css_selector("a[data-id='38579']").click()

Darrell
 

Vilson Teixeira Júnior

unread,
Oct 26, 2017, 10:30:40 AM10/26/17
to webd...@googlegroups.com
Hello

Try

driver.find_element_by_xpath("//*[@class='pkg-button'][@id='38579']).click();



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.



--
Vilson Teixeira Júnior
(51) 98468-3783
Reply all
Reply to author
Forward
0 new messages