首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python 2.7中使用Selenium选择按钮

在Python 2.7中使用Selenium选择按钮
EN

Stack Overflow用户
提问于 2018-07-19 23:49:59
回答 1查看 311关注 0票数 0

我正在抓取的一个网站最近更改了我使用的按钮ID。由于某种原因,我找不到新元素。我已经浏览了多个站点(包括Stack Overflow)来选择一个按钮,但没有一个我尝试过的。我几乎是Selenium的新手。下面是HTML摘录:

代码语言:javascript
复制
                <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>

我试过了

代码语言:javascript
复制
button_id = 'store-search-modal-make-this-my-store-'+shop
make_my_store = driver.find_element_by_id(button_id)

代码语言:javascript
复制
make_my_store = driver.find_element_by_xpath("//button[contains(text(),'Make 
This My Store')]")

结果如下:

代码语言:javascript
复制
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)

代码语言:javascript
复制
    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的源代码,然后看到:

代码语言:javascript
复制
    <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类。所有这些都给出了找不到对象的错误。

EN

回答 1

Stack Overflow用户

发布于 2018-07-20 00:08:24

尝试使用以下xpath:

代码语言:javascript
复制
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找到。

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

代码语言:javascript
复制
    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。例如:

代码语言:javascript
复制
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!") 

您将需要以下导入:

代码语言:javascript
复制
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

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51427008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档