我正在尝试构建一个测试,但似乎找不到(然后单击(页面HTML中具有唯一名称的元素。
它一直工作到这里:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Chrome()
driver.get("https://www.hallmark.nl/kaarten/verjaardag-man/")
wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='https://www.hallmark.nl:443/kaarten/verjaardag-man/grappig-m/hallmark/een-jaguar-voor-je-verjaardag-3346861.aspx']"))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnShowSizepicker"))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Standaard']"))).click()
我正试图点击按钮"Binnenkant"(注意后面的空格(。为此,我尝试过:
elem = driver.find_element_by_class_name('primaryButton').click()
elem = driver.find_element_by_xpath('//button[.="Binnenkant "]').click()
elem = driver.find_element_by_xpath(//*[@id="CardSelectBar"]/div[3]/button[2]/text()).click() #xpath copied from Chrome
但我每次都会得到一个"没有这样的元素"的错误。
这个特定页面上有两个类:showDesktop和hideDesDesktop。两者都包括您搜索的按钮。试试这个Xpath:
"//div[contains(@class, 'showDesktop')]//button[contains(text(),'Binnenkant')]"