-1

I'm using google colab to run stable diffusion and I want to make a txt file with all the prompts and settings. These are located in a div class element under the generated image in the web ui. I want a python code to copy the text (with the promots and settings) and save it in a txt file each time I enter a filename.

This is my code

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('https://1234.gradio.live')

while True:
    folder_path = input("Enter folder path to save text files: ")
    file_name = input("Enter file name to save text: ")
    if file_name == 'stop':
        break
    file_path = folder_path + '\\' + file_name + '.txt'

    transition_div = browser.find_element_by_xpath('//div[@class="transition"]')
    p_elements = transition_div.find_elements_by_tag_name("p")
    p_texts = [p.text for p in p_elements]

    with open(file_path, 'w', encoding='utf-8') as f:
        for p_text in p_texts:
            f.write(p_text + '\n')
        print(f"Text saved in {file_path}")

browser.quit()

This is the error that I'm getting:

Traceback (most recent call last):
  File "C:\User\..\selenium firefox.py", line 13, in <module>
    transition_div = browser.find_element_by_xpath('//div[@class="transition"]')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

How do I fix this?

  • Does this [answer](https://stackoverflow.com/a/75489249/7429447) helps you? – undetected Selenium Feb 26 '23 at 20:21
  • Does this answer your question? [AttributeError: 'WebDriver' object has no attribute 'find\_element\_by\_xpath'](https://stackoverflow.com/questions/72754651/attributeerror-webdriver-object-has-no-attribute-find-element-by-xpath) – JeffC Feb 26 '23 at 23:20

1 Answers1

0

Not sure which version of selenium you use but the syntax looks different in the current version (4.8.2). So it should be something like driver.find_element(By.XPATH, ...)

Have a look at: https://selenium-python.readthedocs.io/locating-elements.html