Python 查找3个div内的按钮
我试图找到一个按钮的文本。这是html布局Python 查找3个div内的按钮,python,selenium,web,screen-scraping,Python,Selenium,Web,Screen Scraping,我试图找到一个按钮的文本。这是html布局 <div id="fulfillment-add-to-cart-button"> <div class="fulfillment"> <div> <div style="position:relative"> <button class="btn ban-disabled">
<div id="fulfillment-add-to-cart-button">
<div class="fulfillment">
<div>
<div style="position:relative">
<button class="btn ban-disabled"> Sold Out </button>
</div>
</div>
</div>
</div>
我一直返回一个空数组。我也尝试过用button类名查找button,但是它也返回一个空数组。button是单个元素,对吗?因此,您可以通过
元素
查找,而不是元素
尝试此操作,在该按钮中搜索特定文本
button_elem = driver.find_element_by_xpath("//div[@id='fulfillment-add-to-cart-button']//button")
print(button_elem.text)
如果这不起作用,我们可能需要看到更多围绕该部分的HTML也许你应该将
div
简化为//并在word元素中使用find\u元素`而不使用chars
,也不使用[0]
-驱动程序。通过xpath(//div[@id='fulfillment-add-to-cart-button']//button)查找元素)
如果页面使用JavaScript添加它,那么您可能需要时间。sleep(0.1)
和JavaScript将有时间添加元素。如果您使用真实URL添加最小的工作代码,那么会简单得多-然后我们可以测试它并看到问题。您在获取第一个div
时有问题吗?可能所有这些HTML都在
中,您可能需要切换到这个框架。有我们可以使用的url吗?
button_elem = driver.find_element_by_xpath("//div[@id='fulfillment-add-to-cart-button']//button")
print(button_elem.text)