首页 > 解决方案 > 如何使用 python/selenium 根据工作簿中的值动态更新 xpath

问题描述

我必须根据 excel 表中给出的年份选择任何年份,现在我直接在代码中给出了年份。能够直接从 excel 表中获取为城市编写的数据,并且对年份进行硬编码也可以。问题有 n 年作为链接(不在下拉列表中)而不是为每一年编写 xpath,有没有办法从 excel 表中获取给出的值并将其放置在给 xpath 中说“年”。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>    

更新 当我尝试下面的方法时,我收到了这个错误。我从 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 图像 Excel 截图

标签: python-3.xseleniumselenium-webdriveranacondaspyder

解决方案


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

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

这将导致使用以下路径:

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

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

下一步是确保将year_from_excel其设置为 Excel 电子表格中的值。根据您现有的代码,我将使用:

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

不过,我对您的 Excel 工作表的格式没有任何见解,因此这可能是错误的。我假设年份值在同一行,但在下一列。

所以如果你把所有这些结合在一起,你会得到:

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

推荐阅读