问题在于,有两个h1
外部元素完全相同的元素HTML
:第一个元素是隐藏的,第二个元素不是。你可以用
print(len(driver.find_elements_by_xpath('//h1[@class="product-title "]')))
text
属性允许您 *
尝试更换
new_name = driver.find_element_by_xpath(xp_name).text
与
new_name = driver.find_element_by_xpath(xp_name).get_attribute('textContent')
或简单地处理第二个(可见)标头:
driver.find_elements_by_xpath('//h1[@class="product-title "]')[1].text