Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用python/selenium根据工作簿中的值动态更新xpath_Python 3.x_Selenium_Selenium Webdriver_Anaconda_Spyder - Fatal编程技术网

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

Python 3.x 如何使用python/selenium根据工作簿中的值动态更新xpath,python-3.x,selenium,selenium-webdriver,anaconda,spyder,Python 3.x,Selenium,Selenium Webdriver,Anaconda,Spyder,我必须根据excel表中给出的年份选择任何年份,因为现在我已经直接用代码给出了年份。可以直接从excel表中获取为城市编写的数据,硬编码年份也可以。问题是,有n个年份作为链接(不在下拉列表中),而不是每年编写xpath,有没有办法从excel表格中获取给定的值,并将其放在“年”位置。 Yearlink=driver.find_element_by_xpath('//span[contains(text(),“year”)]); 我目前正在使用spyder python 3.7 请找到我尝试过的代

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

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

year_from_excel = "2000"
year_link = driver.find_element_by_xpath("//span[normalize-space(.)='{}']".format(year_from_excel))
这将导致使用以下路径:

//span[normalize-space(.)='2000']
这是一个XPath,它将找到一个包含文本
2000
元素,但是在执行比较时,它将去掉所有周围的空白

下一步是确保excel中的
year\u from\u 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()

我能够根据提供的建议找到解决方案。必须为上述解决方案增加“价值”

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

你能分享html的一部分吗dom@VardhmanPatil添加了html的一些部分,希望它足够。当我尝试你建议的方式时,我得到了一个错误,我已经在上面更新了。你能建议问题可能是什么吗。它看起来像
sheet.cell(1,1)
正在返回
text:'1979'
你可以尝试
sheet.cell(1,1).getStringCellValue()
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()