在Python 2.7中使用Selenium选择按钮

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (230)

我最近抓的一个网站改变了我正在使用的按钮ID。出于某种原因,我找不到新元素。我已经阅读了多个网站(包括Stack Overflow)来选择按钮,我尝试的任何工作都没有。我几乎是Selenium的新手。这是HTML提取:

                <div class="info">
                <h4 class="store-number">
                    Store Number: {{=storeId}}
                </h4>
                {{ if (closeForEcommerce == 0 ) { }}
                <button id="store-search-modal-make-this-my-store-{{=storeId}}" class="btn btn-make-this-my-store btn-block btn-primary-2 {{ if (ResultDisplayHelper.isMyStore(storeId)) { print("hidden"); } }}"
                        onclick="ResultDisplayHelper.setMyStoreMarker({{=storeId}});ResultDisplayHelper.setMyStore('store-search-modal-abc-store-card-info-', 'store-search-modal-make-this-my-store-', 'store-search-modal-my-store-', {{=storeId}})">
                    Make This My Store
                </button>
                {{ } }}

                {{ if (closeForEcommerce != 0 ) { }}
                <button id="btnStoreCloseForEcommerce" class="btn btn-store-temporarily-closed btn-block btn-primary-2 {{ if (ResultDisplayHelper.isMyStore(storeId)) { print("hidden"); } }}"
                        onclick="">
                    Store Temporarily Closed
                </button>
                {{ } }}

                <a id="store-search-modal-my-store-{{=storeId}}" href="{{=clickUri}}" class="CoveoResultLink my-store btn btn-gray-300 btn-block {{ if (!ResultDisplayHelper.isMyStore(storeId)) { print("hidden"); } }}">
                    My Store
                </a>
                <a class="CoveoResultLink" href="{{=clickUri}}">Visit Store Page</a>
                <div class="location">
                    {{ if (dist != null) { }}
                    <div><strong>Miles</strong>: {{=ResultDisplayHelper.metersToMiles(dist)}}</div>
                    {{ } }}
                    <address>
                        {{ if (shoppingcenter) { }}
                        {{=shoppingcenter}}<br/>
                        {{ } }}
                        {{=address1}}
                        {{ if (address2) { }}<br />{{=address2}} {{ } }}
                        <br />
                        {{=city}}, {{=state}} {{=zip}}
                    </address>
                </div>

我试过了

button_id = 'store-search-modal-make-this-my-store-'+shop
make_my_store = driver.find_element_by_id(button_id)

make_my_store = driver.find_element_by_xpath("//button[contains(text(),'Make 
This My Store')]")

结果:

NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"store-search-modal-make-this-my-store-231"}
  (Session info: headless chrome=67.0.3396.99)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.17134 x86_64)

    NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"button[onclick^=ResultDisplayHelper]"}
  (Session info: headless chrome=67.0.3396.99)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.17134 x86_64)

我错过了什么?

更新:感谢您的建议到目前为止。不幸的是,当我尝试多种变体时,由于找不到对象,我不断收到超时错误。我抓住了driver.page的来源,看看:

    <button id="make-this-my-store" class="btn btn-block btn-primary-2" 
ng-show="model.store.storeId !== model.abcCartService.cart.pickupStore.storeId &amp;&amp; 
model.store.closeForEcommerce !== 'True'" ng-click="model.makeMyStore();">
        Make This My Store
</button>

我尝试使用XPATH查找“Make This My Store”,使用“make-this-my-store”作为按钮ID,并使用“btn btn-block btn-primary-2”作为CSS类。所有都给对象找不到错误。

提问于
用户回答回答于

根据HTML,您已经将文本元素共享为Make This My Store是一个Angular元素,因此要调用click()所需元素,您必须引入WebDriverWait才能使元素可单击,您可以使用以下解决方案:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-make-this-my-store.btn-block.btn-primary-2[id^=store-search-modal-make-this-my-store-]"))).click()

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
用户回答回答于

尝试使用以下xpath:

button = browser.find_element_by_xpath("//button[@id='store-search-modal-make-this-my-store-{{=storeId}}']")
button.click() 

现在,如果这不起作用,驱动程序可能需要等待几秒才能查找该元素。在这种情况下,我建议使用显式等待。有关显式等待的更多信息,请参见此处= http://selenium-python.readthedocs.io/waits.html

您可以包含显式等待,如下所示:

    xpath = "//button[@id='store-search-modal-make-this-my-store-{{=storeId}}']"
   button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))).click() 

这意味着驱动程序将等待最多10秒钟才能显示该元素并可单击。如果驱动程序找不到该元素,则会抛出TimeoutException。您可以使用try / except块来处理TimeOutException。例如:

try:
   xpath = "//button[@id='store-search-modal-make-this-my-store-{{=storeId}}']"
    button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))).click() 
except TimeoutException:
   print("Button not found!") 

您将需要以下导入:

import selenium.webdriver as webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

希望这可以帮助!

扫码关注云+社区

领取腾讯云代金券