【问题标题】:Why is selenium not finding an element on a website为什么硒在网站上找不到元素
【发布时间】:2021-04-26 12:37:44
【问题描述】:

我有以下要运行的测试。正在从我的申请中提出付款请求。为了让用户完成付款,他们必须访问处理付款的网站上的链接。我希望 selenium 输入完成付款所需的电子邮件地址。我收到以下错误。

E
======================================================================
ERROR: test_submit_payment (order.tests.test_views.TestViewsLiveServer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/patrice/django-apps/dropby/dropby/order/tests/test_views.py", line 222, in test_submit_payment
    username = self.selenium.find_element_by_id('username')
  File "/home/patrice/django-apps/dropby/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/home/patrice/django-apps/dropby/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/home/patrice/django-apps/dropby/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/patrice/django-apps/dropby/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="username"]

下面是我的测试代码。

def test_submit_payment(self):
    # get the url to goto the payment view submission view
    url = self.live_server_url + reverse('order:payment', kwargs={'pk': self.order.pk})
    self.selenium.get(url)
    # self.selenium.find_element_by_xpath("//select[@name='method_of_payment']/option[text()='ecocash']").click()
    phone_number_input = self.selenium.find_element_by_name('phone_number')
    phone_number_input.send_keys('0777777777')
    # Submit the form 
    submit_button = self.selenium.find_element_by_id("submit-payment")
    submit_button.click()
    # Now find the url that goes to the paynow website to complete a payment
    complete_payment_url = self.selenium.find_element_by_id('complete-payment-url')
    complete_payment_url.click()
    # Now we are on the paynow website, enter in the email address of the user who is going
    # To make the payment
    username = self.selenium.find_element_by_id('username')
    username.send_keys('abantusoft@gmail.com')
    # Now take the submit button and click it
    login_button = self.selenium.find_element_by_id('login-next')
    login_button.click()
    # Get the payment that was recently created
    payment = Payment.objects.last()
    # Create a url to visit to confirm the payment
    payment_completion_url = self.live_server_url + reverse('order:complete_payment', kwargs={'pk': payment.pk})
    self.selenium.get(payment_completion_url)
    # find the button to confirm the payment and click it
    confirm_payment_button = self.selenium.find_elements_by_id('confirm-payment-button')
    confirm_payment_button.click()
    # Now that we have confirmed the payment
    # check to see if the payment has been paid
    payment = Payment.objects.last()
    self.assertEquals(payment.paid, True, msg='Payment has not been completed')
    # confirm also that the order is ready
    self.assertEquals(payment.order.is_ready, True, msg="Order is not ready, since payment has not been completed")

我认为该网站可能受硒保护。因为如果我查看页面源代码,我发现我尝试选择的元素的 ID 为 username。我应该能找到它。

<form id="login-email">
    <div class="input-group">
        <label>
            Email Address
            <input id="username" name="username" type="email" value="">
        </label>
        
        <!-- TO ALLOW CHROME TO AUTOFILL USERNAME -->
        <input type="password" style="display: none">
    </div>
    <button type="submit" id="login-next" class="btn">Next</button>
</form>

【问题讨论】:

标签: django selenium django-testing


【解决方案1】:

我猜你需要等到页面加载完毕。
所以添加以下内容:

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


wait.until(EC.visibility_of_element_located((By.ID, "username")))

之前

username = self.selenium.find_element_by_id('username')

【讨论】:

  • Traceback (most recent call last): File "/home/patrice/django-apps/dropby/dropby/order/tests/test_views.py", line 222, in test_submit_payment self.wait(self.selenium, 10).until(EC.presence_of_element_located((By.ID, 'username'))) File "/home/patrice/django-apps/dropby/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: 我现在收到此错误。
  • 你设置了多少秒等待量?
  • 等待 10 秒
  • 尝试添加更多,20 或 30
  • 好吧,让我试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 2020-03-24
  • 1970-01-01
  • 2016-12-17
  • 2021-08-17
  • 2020-08-17
相关资源
最近更新 更多