Skip to content

Instantly share code, notes, and snippets.

@anthony1x6000
Last active December 11, 2021 21:09
Show Gist options
  • Select an option

  • Save anthony1x6000/5b21b83220942c4d1164b47a3599e7b6 to your computer and use it in GitHub Desktop.

Select an option

Save anthony1x6000/5b21b83220942c4d1164b47a3599e7b6 to your computer and use it in GitHub Desktop.
Python for loop, save selenium webdriver get text to a specific file.
websites = ["https://github.com/anthony1x6000/website", "https://github.com/raryelcostasouza/pyTranscriber", "https://github.com/ONLYOFFICE/server"]
textfiles = ["text1.txt", "text2.txt", "text3.txt"]
xpath = '//*[@id="repository-container-header"]/div[1]/div/h1/span[1]/a' # just the single xpath
with suppress(Exception):
for file in textfiles:
os.remove(file) #deletes old text files
for file in textfiles:
open(file, 'a').close()# creates the same file
v = 0
for site in websites:
print("v(ListCount) =", v)
browser.get(site)
xpathtext = browser.find_element_by_xpath(xpath)
print('Got text')
print("Before filewrite, xpathtext =", xpathtext.text)
with open(textfiles[v], "w") as f:
print(xpathtext.text, file=f)
print("After filewrite, xpathtext =", xpathtext.text)
for file in textfiles:
with open(file, 'r') as f:
print("File contents =", file, "=", f.read())
v = v + 1
browser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment