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!
python selenium xpath web-scraping selenium-chromedriver
add a comment |
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!
python selenium xpath web-scraping selenium-chromedriver
add a comment |
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!
python selenium xpath web-scraping selenium-chromedriver
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
python selenium xpath web-scraping selenium-chromedriver
asked Nov 18 at 20:31
A.E
1419
1419
add a comment |
add a comment |
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
@Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
– A.E
Nov 18 at 21:27
add a comment |
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.
@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
add a comment |
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
@Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
– A.E
Nov 18 at 21:27
add a comment |
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
@Andersson-Thanks and sorry for my mistake in using ancestor; I am just learning selenium
– A.E
Nov 18 at 21:27
add a comment |
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
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
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
add a comment |
@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
add a comment |
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.
@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
add a comment |
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.
@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
add a comment |
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.
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.
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
add a comment |
@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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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