5

I got a problem to use "find_element_by_css_selector" to get the element "Select" (a href).

I tried the methods below but all of them didn't work:

driver.find_element_by_css_selector("div.plan.right > a.select.").click()
driver.find_element_by_xpath("//div[@class='plan right']/div[2]/a/select").click()

Could anyone kindly give me some suggestions? Thanks!!

<div class="choose_plan">
   <h1>Sign up now for <strong>UNLIMITED</strong> access <br/>to all </h1>
   <div class="plans">
      <div class="plan left">
         <div class="head">
            <p>MONTHLY</p>
         </div>
         <div class="body">
            <p>annually</p>
         </div>
         <hr />
         <a href="/bbb?plan_id=6" class="select signup-dialog" data-planId="6" data-url="/users/new?r">SELECT</a>
      </div>
      <div class="plan right">
         <img alt="Popular-right" class="popular" src="/assetse8.png" />
         <div class="head">
            <p>14</p>
         </div>
         <div class="body">
            <p>Unlimited</p>
         </div>
         <hr />
         <a href="/account/purchase?plan_id=31" class="select signup-dialog" data-planId="31" data-url="/users/new?aaa">SELECT</a>
      </div>
   </div>
</div>
5
  • Can you please format your code. Is it the anchor tag (A) that you want selected? Commented Oct 1, 2015 at 2:14
  • Hi, e4c5, Ihave tried to format the code. Yes, I want to select "<a href="/bbb?plan_id=6" class="select signup-dialog" data-planId="6" data-url="/users/new?r">SELECT</a>" which under class "plan right". Commented Oct 1, 2015 at 2:33
  • How about find_element_by_link_text() ? Commented Oct 1, 2015 at 2:34
  • Because of I have another a href link under class "plan left", there are two "select", so, how do link_text() to figure out to click the one I want? Commented Oct 1, 2015 at 2:39
  • Welcome to Stack Overflow! When you post HTML please take a minute to use a beautifier like jsbeautifier.org to properly format it. It makes it a LOT easier to read which makes your question more likely to get answered. Thanks! Commented Oct 6, 2015 at 1:00

3 Answers 3

5

I know you already have an answer but there's a simple alternative that may be helpful in the future. From the HTML you provided, it looks like the data-planId attribute is unique for each A tag. You can take advantage of that using the code below.

driver.find_element_by_css_selector("a[data-planId='31']")

See this CSS Selector reference for more info.

Sign up to request clarification or add additional context in comments.

Comments

1

It would help to have well formed HTML, as line 15 (<div class="choose_plan">) appears to be unclosed. This solution below was done with this line removed, but the rest of the HTML as shown. You can test online XPath here.

driver.find_element_by_xpath("//div[@class='plan right']/a").click()

yields the following:

Element='<a href="/account/purchase?plan_id=31" class="select signup-dialog" data-planId="31" data-url="/users/new?aaa">SELECT</a>'

3 Comments

Hi Thane, driver.find_element_by_xpath("//div[@class='plan right']/a").click() is working! Thank you very much! And the link online XPath is very useful!
@betty Glad to help Betty. That link has saved me lots of time in figuring out XPath queries.
sorry may i know this : Element='<a href="/account/purchase?plan_id=31" class="select signup-dialog" data-planId="31" data-url="/users/new?aaa">SELECT</a>' Whether i need to insert this in my code?
1

I would try to make it simple:

driver.find_element_by_css_selector("div.right a.select")

Or:

driver.find_elements_by_link_text("SELECT")[-1]

Here we are basically getting the last a element having SELECT text.

2 Comments

Hi alecxe, I tried the two methods and they didn't work after I tested. Since I got the answer from Thane, I'd like to thank you for spending time for my question.
@betty interesting. Still don't see why the other answer worked and the options here didn't. Anyway, glad to see the problem solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.