-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathupper_case_xpath.py
56 lines (49 loc) · 2.03 KB
/
upper_case_xpath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
from __future__ import print_function
import os
import sys
import re
import time
from os import getenv
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import InvalidSelectorException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
is_windows = getenv('OS') != None and re.compile('.*NT').match( getenv('OS'))
homedir = getenv('USERPROFILE' if is_windows else 'HOME')
chromedriver_path = homedir + os.sep + 'Downloads' + os.sep + ('chromedriver.exe' if is_windows else 'chromedriver')
options = Options()
driver = webdriver.Chrome(executable_path = chromedriver_path, options = options)
if driver != None:
driver.get('https://www.seleniumeasy.com/test/')
url = 'https://crossbrowsertesting.com/?utm_source=seleniumeasy&utm_medium=da&utm_campaign=sedemo'
url_fragment = 'https://crossBrowsertesting.com'
# https://stackoverflow.com/questions/24183701/xpath-lowercase-is-there-xpath-function-to-do-this
# https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/translate
xpaths = [
'//a[contains(@href,"{}")]'.format(url_fragment.lower()),
'//a[contains(lower-case(@href),"{}")]'.format(url_fragment),
'//a[contains(translate(@href, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), "{}")]'.format(url_fragment.upper())
]
length = len(xpaths)
for cnt in range(length):
xpath = xpaths[cnt]
print('# try {}'.format(cnt))
element = None
try:
element = driver.find_element_by_xpath(xpath)
except (InvalidSelectorException, NoSuchElementException) as e:
print('Exception (ignored): {}'.format(e))
pass
if element != None:
print('Found via {}'.format(xpath))
print(element.get_attribute('outerHTML'))
else:
print('Failed via {}'.format(xpath))
time.sleep(5)
driver.close()
driver.quit()