我有以下要运行的测试。正在从我的申请中提出付款请求。为了让用户完成支付,他们必须访问处理支付的网站上的链接。我希望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>发布于 2021-04-26 20:51:40
我想您需要等待页面加载完成。
因此,添加以下内容:
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')https://stackoverflow.com/questions/67266777
复制相似问题