Warm tip: This article is reproduced from stackoverflow.com, please click
Anaconda python-3.x selenium selenium-webdriver spyder

How to update an xpath dynamically based on the value from workbook using python/selenium

发布于 2020-03-27 10:20:20

I have to select any year depending on the year given in the excel sheet,for now i have given the year directly in code.Able to get the data written for city directly from the excel sheet and hard coding the year also works.The issue there are n number of years as links (not in drop downs)instead of writing xpath for each and every year,is there any way to get the value given from the excel sheet and place it the give xpath say "year". Yearlink=driver.find_element_by_xpath('//span[contains(text(),"year")]'); I am currently using spyder-python 3.7

Please find the code that i have tried

driver = webdriver.Chrome  
driver.fullscreen_window();
driver.get('url');
time.sleep(5) 
Workbook=xlrd.open_workbook("excel path")
Details = Workbook.sheet_by_index(0);
city=Details .cell(1,0)
Citytextbox=driver.find_element_by_xpath('//input[@id="city"]');
Citytextbox.send_keys(city.value);
Yearlink=driver.find_element_by_xpath('//span[contains(text(),"2000")]');
Yearlink.click();
time.sleep(10)

Update:Adding some part of html ,if there are any other methods do it also do let me know

<span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1910

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1911

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1912

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1913

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1914

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1915

      </span>

    </li><li _ngcontent-c26="" class="option">

      <span _ngcontent-c26="" class="toggle link" ng-reflect-klass="toggle link" ng-reflect-ng-class="">

        1916

      </span>    

UPDATE When i try the below method i am getting this error.I am passing the value 1979 from excel,i am passing '1979 from excel if i don't add ' it takes it as numeric value.

InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //span[normalize-space(.)='text:'1979''] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[normalize-space(.)='text:'1979'']' is not a valid XPath expression. 

Excel image Excel screenshots

Questioner
saranya
Viewed
150
saranya 2019-07-16 16:32

I was able to find a solution based on the suggestions provided.Have to add "Value" for the above solution to work

workbook = xlrd.open_workbook("excel path")
sheet = workbook.sheet_by_index(0)
year_from_excel = sheet.cell(1, 1)
year_link = driver.find_element_by_xpath("//span[normalizespace(.)='{}']".format(year_from_excel.Value))
year_link.click()