how to get unique XPATH for buttons when the features for all input are the same











up vote
1
down vote

favorite












I am trying to extract NBA players stats using selenium web driver in python and here is my attempt:



from selenium import webdriver
from selenium.webdriver.support.ui import Select


browser = webdriver.Chrome()

browser.get('https://www.basketball-reference.com')

xp_1 = "//select[@id='selector_0' and @name='team_val']"
team = Select(browser.find_element_by_xpath(xp_1))
team.select_by_visible_text('Golden State Warriors')

xp_2 = "//select[@id='selector_0' and @name='1']"
player = Select(browser.find_element_by_xpath(xp_2))
player.select_by_visible_text('Jordan Bell')


The problem I have is there are 4 "Go" buttons in this page and all have the same input features. In other words, the following xpath returns 4 buttons:



//input[@type='submit'and @name="go_button" and @id="go_button" and @value="Go!"]


I unsuccessfully tried adding ancestor as below but it does not return an xpath:



//input[@type='submit' and @name="go_button" and @id="go_button" and @value="Go!"]/ancestor::/form[@id='player_roster']


I appreciate any insight!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am trying to extract NBA players stats using selenium web driver in python and here is my attempt:



    from selenium import webdriver
    from selenium.webdriver.support.ui import Select


    browser = webdriver.Chrome()

    browser.get('https://www.basketball-reference.com')

    xp_1 = "//select[@id='selector_0' and @name='team_val']"
    team = Select(browser.find_element_by_xpath(xp_1))
    team.select_by_visible_text('Golden State Warriors')

    xp_2 = "//select[@id='selector_0' and @name='1']"
    player = Select(browser.find_element_by_xpath(xp_2))
    player.select_by_visible_text('Jordan Bell')


    The problem I have is there are 4 "Go" buttons in this page and all have the same input features. In other words, the following xpath returns 4 buttons:



    //input[@type='submit'and @name="go_button" and @id="go_button" and @value="Go!"]


    I unsuccessfully tried adding ancestor as below but it does not return an xpath:



    //input[@type='submit' and @name="go_button" and @id="go_button" and @value="Go!"]/ancestor::/form[@id='player_roster']


    I appreciate any insight!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to extract NBA players stats using selenium web driver in python and here is my attempt:



      from selenium import webdriver
      from selenium.webdriver.support.ui import Select


      browser = webdriver.Chrome()

      browser.get('https://www.basketball-reference.com')

      xp_1 = "//select[@id='selector_0' and @name='team_val']"
      team = Select(browser.find_element_by_xpath(xp_1))
      team.select_by_visible_text('Golden State Warriors')

      xp_2 = "//select[@id='selector_0' and @name='1']"
      player = Select(browser.find_element_by_xpath(xp_2))
      player.select_by_visible_text('Jordan Bell')


      The problem I have is there are 4 "Go" buttons in this page and all have the same input features. In other words, the following xpath returns 4 buttons:



      //input[@type='submit'and @name="go_button" and @id="go_button" and @value="Go!"]


      I unsuccessfully tried adding ancestor as below but it does not return an xpath:



      //input[@type='submit' and @name="go_button" and @id="go_button" and @value="Go!"]/ancestor::/form[@id='player_roster']


      I appreciate any insight!










      share|improve this question













      I am trying to extract NBA players stats using selenium web driver in python and here is my attempt:



      from selenium import webdriver
      from selenium.webdriver.support.ui import Select


      browser = webdriver.Chrome()

      browser.get('https://www.basketball-reference.com')

      xp_1 = "//select[@id='selector_0' and @name='team_val']"
      team = Select(browser.find_element_by_xpath(xp_1))
      team.select_by_visible_text('Golden State Warriors')

      xp_2 = "//select[@id='selector_0' and @name='1']"
      player = Select(browser.find_element_by_xpath(xp_2))
      player.select_by_visible_text('Jordan Bell')


      The problem I have is there are 4 "Go" buttons in this page and all have the same input features. In other words, the following xpath returns 4 buttons:



      //input[@type='submit'and @name="go_button" and @id="go_button" and @value="Go!"]


      I unsuccessfully tried adding ancestor as below but it does not return an xpath:



      //input[@type='submit' and @name="go_button" and @id="go_button" and @value="Go!"]/ancestor::/form[@id='player_roster']


      I appreciate any insight!







      python selenium xpath web-scraping selenium-chromedriver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 at 20:31









      A.E

      1419




      1419
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Try below XPAth to select required Go button:



          "//input[@value='Go!' and ancestor::form[@id='player_roster']]"


          or



          "//form[@id='player_roster']//input[@value='Go!']"


          Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is



          //descendant_node/ancestor::ancestor_node





          share|improve this answer





















          • @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
            – A.E
            Nov 18 at 21:27


















          up vote
          1
          down vote













          You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button



          #player_roster #go_button


          That is



          browser.find_element_by_css_selector("#player_roster #go_button")


          The # is an id selector.



          CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.






          share|improve this answer























          • @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
            – A.E
            Nov 19 at 17:05










          • The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
            – QHarr
            Nov 19 at 17:10













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365150%2fhow-to-get-unique-xpath-for-buttons-when-the-features-for-all-input-are-the-same%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Try below XPAth to select required Go button:



          "//input[@value='Go!' and ancestor::form[@id='player_roster']]"


          or



          "//form[@id='player_roster']//input[@value='Go!']"


          Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is



          //descendant_node/ancestor::ancestor_node





          share|improve this answer





















          • @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
            – A.E
            Nov 18 at 21:27















          up vote
          2
          down vote



          accepted










          Try below XPAth to select required Go button:



          "//input[@value='Go!' and ancestor::form[@id='player_roster']]"


          or



          "//form[@id='player_roster']//input[@value='Go!']"


          Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is



          //descendant_node/ancestor::ancestor_node





          share|improve this answer





















          • @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
            – A.E
            Nov 18 at 21:27













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Try below XPAth to select required Go button:



          "//input[@value='Go!' and ancestor::form[@id='player_roster']]"


          or



          "//form[@id='player_roster']//input[@value='Go!']"


          Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is



          //descendant_node/ancestor::ancestor_node





          share|improve this answer












          Try below XPAth to select required Go button:



          "//input[@value='Go!' and ancestor::form[@id='player_roster']]"


          or



          "//form[@id='player_roster']//input[@value='Go!']"


          Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is



          //descendant_node/ancestor::ancestor_node






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 21:01









          Andersson

          35.9k103066




          35.9k103066












          • @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
            – A.E
            Nov 18 at 21:27


















          • @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
            – A.E
            Nov 18 at 21:27
















          @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
          – A.E
          Nov 18 at 21:27




          @Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
          – A.E
          Nov 18 at 21:27












          up vote
          1
          down vote













          You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button



          #player_roster #go_button


          That is



          browser.find_element_by_css_selector("#player_roster #go_button")


          The # is an id selector.



          CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.






          share|improve this answer























          • @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
            – A.E
            Nov 19 at 17:05










          • The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
            – QHarr
            Nov 19 at 17:10

















          up vote
          1
          down vote













          You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button



          #player_roster #go_button


          That is



          browser.find_element_by_css_selector("#player_roster #go_button")


          The # is an id selector.



          CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.






          share|improve this answer























          • @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
            – A.E
            Nov 19 at 17:05










          • The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
            – QHarr
            Nov 19 at 17:10















          up vote
          1
          down vote










          up vote
          1
          down vote









          You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button



          #player_roster #go_button


          That is



          browser.find_element_by_css_selector("#player_roster #go_button")


          The # is an id selector.



          CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.






          share|improve this answer














          You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button



          #player_roster #go_button


          That is



          browser.find_element_by_css_selector("#player_roster #go_button")


          The # is an id selector.



          CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 17:12

























          answered Nov 19 at 15:08









          QHarr

          28.1k81839




          28.1k81839












          • @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
            – A.E
            Nov 19 at 17:05










          • The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
            – QHarr
            Nov 19 at 17:10




















          • @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
            – A.E
            Nov 19 at 17:05










          • The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
            – QHarr
            Nov 19 at 17:10


















          @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
          – A.E
          Nov 19 at 17:05




          @QHarr-thanks for the suggestion. What are the pros/cons of CSS selector over xpath?
          – A.E
          Nov 19 at 17:05












          The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
          – QHarr
          Nov 19 at 17:10






          The CSS selector is faster than XPath in most instances and generally more robust. stackoverflow.com/a/52039249/6241235
          – QHarr
          Nov 19 at 17:10




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365150%2fhow-to-get-unique-xpath-for-buttons-when-the-features-for-all-input-are-the-same%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin