我有个问题。我想在这里点击使用硒的Zadanie 1
:
https://buggy-testingcup.pgs-soft.com/
我尝试了xpath、部分链接、链接文本等等,但一直以来我都有错误
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div[2]/div[1]/a/h2"}
Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Zadanie 1"}
如何定位此链接?
<a href="/task_1">
<h2>Zadanie 1</h2>
</a>
这对我来说很奇怪,因为当我运行@0m3r的代码时,它是有效的。但是当我把它粘贴到我的项目中时,我会出现错误。
import unittest
import time
import HtmlTestRunner
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from ASTA.Pages.HomePage import ASTA_homepage
class test_task1(unittest.TestCase):
@classmethod
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def test_open_task(self):
driver = self.driver
driver.get("https://asta.pgs-soft.com/")
open_app = ASTA_homepage(driver)
open_app.open_buggy_app()
time.sleep(3)
open_app.open_task1()
@classmethod
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Users/Warpath/PycharmProjects/allegro_tests/venv/ASTA'))
class ASTA_homepage():
def __init__(self, driver):
self.driver = driver
def open_buggy_app(self):
self.driver.find_element_by_partial_link_text("Buggy app").click()
def open_task1(self):
link = self.driver.find_element_by_link_text("Zadanie 1")
link.click()
通过链接文本查找
示例
import time
from selenium import webdriver
chrome_browser = webdriver.Chrome()
url = "https://buggy-testingcup.pgs-soft.com/"
chrome_browser.get(url)
time.sleep(3)
link = chrome_browser.find_element_by_link_text("Zadanie 1")
link.click()
尝试使用除find_element_by_xpath
之外的Selenium方法。您可以找到xpath、jspath、内部HTML等的元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
这些是Selenium允许的可用方法。
我有一个表单填充应用程序,其中每个元素都可以按名称找到,但由于某种原因,其中一个元素需要由xpath独家找到。
我可以使用下面的按钮点击该按钮。此外,在尝试查找元素或与元素交互时,使用等待始终是一种很好的做法。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
# You will need to use the path for your driver
driver = webdriver.Chrome('/Users/Shared/Drivers/chromedriver')
driver.get('https://buggy-testingcup.pgs-soft.com/')
button = wait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[href="/task_1"]')))
button.click()
您可以在此处找到有关使用等待的更多信息:https://selenium-python.readthedocs.io/waits.html