如何使用 python/selenium 根据工作簿中的值动态更新 xpath

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

我必须 select 任何一年,具体取决于 excel sheet 中给出的年份,现在我直接在 code.Able 中给出年份以获得直接从 excel sheet 为城市写入的数据和硬编码年份也 works.The 问题有 n 年作为链接(不在下拉列表中)而不是为每个和编写 xpath每年,有没有办法从 excel sheet 中获取给定的值并将其放置在给定的 xpath 中,比如 "year"。 Yearlink=driver.find_element_by_xpath('//span[contains(text(),"year")]'); 我目前使用的是 spyder-python 3.7

请找到我试过的代码

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)

更新:添加html的一部分,如果还有其他方法也请告诉我

<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>    

更新 当我尝试下面的方法时,我得到这个 error.I 从 excel 传递值 1979,我从 excel 传递 '1979 如果我不添加 ' 它将它作为数字值。

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 图片

从定位器的角度来看,您最希望这样做:

year_from_excel = "2000"
year_link = driver.find_element_by_xpath("//span[normalize-space(.)='{}']".format(year_from_excel))

这将导致使用以下路径:

//span[normalize-space(.)='2000']

这是一个 XPath,它将找到具有文本 2000<span> 元素,但是在执行比较时它会去除所有周围的空白。

下一步是确保 year_from_excel 设置为您的 Excel 价差 sheet 中的一个值。根据您现有的代码,我将选择:

workbook = xlrd.open_workbook("excel path")
sheet = workbook.sheet_by_index(0)
year_from_excel = sheet.cell(1, 1)

我不知道你的 Excel sheet 是如何格式化的,所以这可能是错误的。我假设年份值在同一行,但在下一列。

因此,如果将所有这些放在一起,您将得到:

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[normalize-space(.)='{}']".format(year_from_excel))
year_link.click()

我能够根据建议 provided.Have 找到解决方案,添加 "Value" 使上述解决方案生效

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()