使用Selenium和Python,但打印功能不起作用



我正在使用selenium编写一个测试脚本来自动购买一些项目。然而,由于某种原因,当我要求python打印某个元素文本时,控制台中没有出现任何东西来断言我的脚本已经选择了项目的正确颜色。

# Driver select the first trainer option visible on the page
driver.find_element(By.XPATH,"//img[@title='Fresh Foam X 1080v12, M1080Z12']").click()
# Driver click on the orange version of these trainers
driver.find_element(By.XPATH,"//button[contains(@title,'M1080M12')]//span[contains(@class,'p-auto')]").click()
# Make sure you have the correct colour
trainerColour1 = driver.find_element(By.XPATH,"//span[@class='display-color-name color-name-mobile font-body regular pdp-update-event-triggerd']").text
print (trainerColour1) # For some reason its not printing this element on the log
#assert "apricot" in trainerColour1

在这一点上,我期待着"充满春潮的橙色和充满活力的杏";出现在控制台日志上,让我断言"杏子"这个词;以确保选择了正确的颜色。然而,控制台上什么也没有出现。控制台结果

如果文本不在元素上,而是在元素的子元素上,则将。text更改为。get_attribute("innerText")

其他解决方案可能对您想要提取的元素更准确,但有时您需要这样做,因为文本混合在多个元素

trainerColour1 = driver.find_element(By.XPATH,"//span[@class='display-color-name color-name-mobile font-body regular pdp-update-event-triggerd']").get_attribute("innerText") 

最新更新