How to write Xpath for dynamic value passing?
Question:
How to write Xpath for the following:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze
("1622662"," SP0001622662","CS3_pro2_axeda6","5336293761");">
Highlighted values are there in some variable(st_name). Highlighted and Red colour rounded values will be changing dynamically.
I’m not able to get how to write Xpath for this.
import xlrd
path = r'C:UserstmouPycharmProjectsPythonWebScrapingBook2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
try:
if st == float(st):
st_string = int(st)
#variable = 1622662
#new_string = "javascript:popAnalyze("" + str(st_string) + "","SP0001622662","CS3_pro2_axeda6","5336293761");"
#driver.find_element_by_xpath("//input[@class='t2']/@onclick='" + st_string + "'").click()
#driver.find_element_by_xpath("//input[@value='Analyze' and contains(@onclick='" + st_string + "']")
#driver.find_element_by_xpath("//a[@title='" + st_string + "']")
HTML:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze("1622662","SP0001622662","CS3_pro2_axeda6","5336293761");">
Answers:
There are many ways to do this without the value of onclick
, so don’t bother even if it is dynamic, like shown below:
//input[@title='Jump to Detailed Analysis']
or
//input[@value='Analyze']
or
//input[@value='Analyze' and @title='Jump to Detailed Analysis']
EDIT 1:
You can use variable like shown below:
variable = "Analyze"
xpath = "//input[@value='" + variable + "']"
EDIT 2:
variable = 1622662
new_string = "javascript:popAnalyze("" + str(variable) + "","SP0001622662","CS3_pro2_axeda6","5336293761");"
EDIT 3:
variable = 1622662
xpath = "//input[@value='Analyze' and contains(@onclick,'" + str(variable) + "')]"
if driver.find_elements_by_xpath(xpath):
driver.find_element_by_xpath(xpath).click()
- In the above code
variable
will be your dynamic value.
xpath
variable will have a dynamic xpath based on the value of
variable
if driver.find_elements_by_xpath(xpath):
will check if at least one
element with the xpath exit
- if exists exits click on it
Use one of the following XPath :
//input[@value='Analyze' and contains(@onclick,'"+st_name+"')]
OR
//input[@title='Jump to Detailed Analysis' and contains(@onclick,'"+st_name+"')]
Final Code :
driver.find_element_by_xpath("//input[@title='Jump to Detailed Analysis' and contains(@onclick,'"+st_name+"')]")
If the value that you are looking for is the one under onclick attribute then the following Xpath expression should work:
string(//input[@class='t2']/@onclick)
Edit 1
Can you try for XPath version 3 or lower:
//input[(@class='t2' and matches(@onclick,'1622662'))]
And for XPath version 3.1:
//input[@class='t2']/[matches(@onclick, '1622662')]
How to write Xpath for the following:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze
("1622662"," SP0001622662","CS3_pro2_axeda6","5336293761");">
Highlighted values are there in some variable(st_name). Highlighted and Red colour rounded values will be changing dynamically.
I’m not able to get how to write Xpath for this.
import xlrd
path = r'C:UserstmouPycharmProjectsPythonWebScrapingBook2.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
try:
if st == float(st):
st_string = int(st)
#variable = 1622662
#new_string = "javascript:popAnalyze("" + str(st_string) + "","SP0001622662","CS3_pro2_axeda6","5336293761");"
#driver.find_element_by_xpath("//input[@class='t2']/@onclick='" + st_string + "'").click()
#driver.find_element_by_xpath("//input[@value='Analyze' and contains(@onclick='" + st_string + "']")
#driver.find_element_by_xpath("//a[@title='" + st_string + "']")
HTML:
<input class="t2" style="background-color:#008000;" title="Jump to Detailed Analysis" type="button" value="Analyze" onclick="javascript:popAnalyze("1622662","SP0001622662","CS3_pro2_axeda6","5336293761");">
There are many ways to do this without the value of onclick
, so don’t bother even if it is dynamic, like shown below:
//input[@title='Jump to Detailed Analysis']
or
//input[@value='Analyze']
or
//input[@value='Analyze' and @title='Jump to Detailed Analysis']
EDIT 1:
You can use variable like shown below:
variable = "Analyze"
xpath = "//input[@value='" + variable + "']"
EDIT 2:
variable = 1622662
new_string = "javascript:popAnalyze("" + str(variable) + "","SP0001622662","CS3_pro2_axeda6","5336293761");"
EDIT 3:
variable = 1622662
xpath = "//input[@value='Analyze' and contains(@onclick,'" + str(variable) + "')]"
if driver.find_elements_by_xpath(xpath):
driver.find_element_by_xpath(xpath).click()
- In the above code
variable
will be your dynamic value. xpath
variable will have a dynamic xpath based on the value of
variable
if driver.find_elements_by_xpath(xpath):
will check if at least one
element with the xpath exit- if exists exits click on it
Use one of the following XPath :
//input[@value='Analyze' and contains(@onclick,'"+st_name+"')]
OR
//input[@title='Jump to Detailed Analysis' and contains(@onclick,'"+st_name+"')]
Final Code :
driver.find_element_by_xpath("//input[@title='Jump to Detailed Analysis' and contains(@onclick,'"+st_name+"')]")
If the value that you are looking for is the one under onclick attribute then the following Xpath expression should work:
string(//input[@class='t2']/@onclick)
Edit 1
Can you try for XPath version 3 or lower:
//input[(@class='t2' and matches(@onclick,'1622662'))]
And for XPath version 3.1:
//input[@class='t2']/[matches(@onclick, '1622662')]