【问题标题】:I don't understand what "driver.find_element_by_..." command I have to use - could someone help me?我不明白我必须使用什么“driver.find_element_by_...”命令 - 有人可以帮助我吗?
【发布时间】:2021-06-05 08:53:18
【问题描述】:

我想调查以下页面:https://jackpot-qr.rewe.de/?c=939393938 并希望 Selenium 在您按“确定”时显示红色错误代码。

到目前为止,这是我的代码,但它从未找到代码(我是 python 新手,afaik 它只会在“try:”不起作用时返回“except:”-错误消息)

try:
    driver.find_element_by_class_name("code_error")
    print("Code failed")
    time.sleep(10)

except:
    print("Didn't work, sorry")

我做错了什么?

【问题讨论】:

  • 你到底得到了什么错误?你能用错误更新你的问题吗?
  • 另外,你是不是要获取红色错误码的文字?
  • 此外,如果您像这样捕获异常,您将永远不会知道实际发生了什么错误。您应该先尝试except Exception as e: print(str(e)),以确定实际发生了什么错误

标签: python selenium webdriver


【解决方案1】:

你应该看看Explicit Waits

当您单击“确定”时,显示错误代码的元素不会立即出现。因此,我们需要等到它出现后,您才能继续处理其余代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Other code to initialize driver

# ...

# Click OK button
driver.find_element(By.XPATH, '/html/body/div[1]/main/section[1]/div[1]/form/div/button').click()

# Wait till code error appears
WebDriverWait(driver, 30).until(
    EC.presence_of_element_located((By.CLASS_NAME, "code__error")))

# Print code error
print(driver.find_element_by_class_name("code_error").text)

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2014-04-19
    • 2021-08-13
    相关资源
    最近更新 更多