Gonzalo Gonzalo - 6 months ago 54
Python Question

Selenium find_element_by_xpath not working for SharePoint 2013

I would like to configure Selenium to open a SharePoint 2013 list and download it (export to excel). I can have selenium clicking in the List to show the option to Export to Excel, but when I use the xpath to click in Export to Excel I get an error sayng it doesnt find the xpath. Any idea why?

Html code of the button is:

<span unselectable="on" class="ms-cui-ctl-largelabel">Export to<br>Excel</span>


Xpath is:

//*[@id="Ribbon.List.Actions.ExportToSpreadsheet-Medium"]/span[2]


Code and Print below.

Thanks.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

path_to_Ie = 'C:\\Python34\\IEDriver\\IEDriverServer.exe' # change path as needed
browser = webdriver.Ie(executable_path = path_to_Ie)
url = 'https://group.shareplace.test/sites/site2/Lists/Actions/AllItems.aspx'
browser.get(url)

time.sleep(2)

browser.find_element_by_xpath("//*[@id='Ribbon.List-title']/a/span[1]").click()

time.sleep(4)


browser.find_element_by_xpath("//*[@id='Ribbon.List.Actions.ExportToSpreadsheet-Medium']/span[2]").click()


enter image description here

Answer

Try below code and let me know if it's not working properly:

xpath = '//span[@class="ms-cui-ctl-largelabel"][contains(text(), "Export to")]'
browser.find_element_by_xpath(xpath).click()
Comments