无法通过类名称或XPath Selenium Python查找元素

时间:2018-11-09 22:37:00

标签: python-3.x selenium selenium-chromedriver

我正在尝试构建测试,但是我似乎找不到(然后单击)页面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

但是我每次都会收到“没有这样的元素”错误。

1 个答案:

答案 0 :(得分:1)

在此特定页面上有2个类:showDesktop和hideDesktop。两者都包含您要搜索的按钮。试试这个Xpath:

"//div[contains(@class, 'showDesktop')]//button[contains(text(),'Binnenkant')]"