S
S
StenMarsh13372020-12-11 14:19:07
Python
StenMarsh1337, 2020-12-11 14:19:07

selenium+telegram?

Hello everyone, I can't make the transition between def!

def driver(message):
    today = datetime.datetime.today()
    chat_id = message.chat.id
    user_id = message.from_user.id
    user = user_data[user_id]
    myDates = today.strftime("%Y-%m-%d 00:00:00")
    ersq = datetime.datetime.strptime(myDates, "%Y-%m-%d %H:%M:%S").timestamp()
    ghsa = float(ersq) * 1000
    url = "" % (
        ghsa, user.time)
    opts = Options()
    opts.add_argument(
        "user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
    opts.add_argument("--user-data-dir=Users/Profile 1")
    opts.add_argument("--profile-directory=Profile 1")
    driver = webdriver.Chrome(options=opts)
    driver.get(url)
    time.sleep(15)
    elem = driver.find_element_by_name("")
    elem.send_keys(" ")
    time.sleep(4)
    pag.hotkey('ctrl', 'a')
    time.sleep(4)
    pag.hotkey('backspace')
    time.sleep(4)
    elem.send_keys()
    time.sleep(4)
    elem = driver.find_element_by_name("accountPassword")
    elem.send_keys()
    time.sleep(4)
    elem = driver.find_element_by_id("continueFieldbutton").click()
    elem = driver.find_element_by_id("send-code-to-trusted-device").click()
    elem = driver.find_element_by_name("digit1")
    msg = bot.send_message(chat_id, "Напиши код который тебе пришёл, у тебя есть 20сек.")
    bot.register_next_step_handler(msg, third)
def third(message):
    elem = driver.find_element_by_name("digit1")
    text_code = message.text
    time.sleep(4)
    elem.send_keys(text_code)
    time.sleep(4)
    elem = driver.find_element_by_id("setupLink").click()
    time.sleep(50)
    pag.hotkey('ctrl', 'a')
    time.sleep(4)
    pag.hotkey('ctrl', 'c')
    time.sleep(10)
    driver.execute_script(
        "window.open('')")
    time.sleep(50)
    pag.hotkey('delete')
    time.sleep(40)
    pag.hotkey('ctrl', 'w')
    time.sleep(10)
    driver.execute_script(
        "window.open('')")
    time.sleep(50)
    pag.hotkey('ctrl', 'v')
    time.sleep(30)
    driver.quit()

It gives the following error "AttributeError: 'function' object has no attribute 'find_element_by_name'"
How to make a transition? so that the driver is not lost, if the driver is inserted into def third, then it opens a new browser, and I need to continue working in the browser that was originally opened and the text was saved when typing in the telegram

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-12-11
@StenMarsh1337

Name the variable and the function differently, why the function is called the driver, like the variable
Well, you will need to pass the driver itself to the third function.

# ...
bot.register_next_step_handler(msg, third, driver)
# ...

# ...
def third(message, driver):
# ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question