在 Python 2.7 中使用 Selenium 选择按钮

Selecting button using Selenium in Python 2.7

我最近抓取的一个网站更改了我使用的按钮 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 class。都给出对象未找到的错误。

您可以尝试使用此 xpath 并且可以使用 explicit 等待:

点击 将此设为我的商店 按钮:

wait = WebDriverWait(driver,20)  
make_this_my_store = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),'Make This My Store')]')))
make_this_my_store.click()  

点击 商店暂时关闭 按钮:

store_temporarily_closed= wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),'Store Temporarily Closed')]')))  
store_temporarily_closed.click()

确保导入这些:

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

解释

显式等待是您定义的代码,用于等待特定条件发生,然后再继续执行代码。最坏的情况是 Thread.sleep(),它将条件设置为等待的确切时间段。提供了一些方便的方法,可帮助您编写仅在需要时等待的代码。 WebDriverWait 结合 ExpectedCondition 是实现此目的的一种方式。

更多关于显式等待,可以找到here

正如你所说

//button[contains(text(),'Make This My Store')]  

不工作。

以防万一,如果您想使用 css 选择器 :

那就是:

h4.store-number+button[class*='btn btn-make-this-my-store btn-block btn-primary-2'][id*='store-search-modal-make-this-my-store']  

code 中类似:(点击 将此设为我的商店

make_this_my_store = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'h4.store-number+button[class*='btn btn-make-this-my-store btn-block btn-primary-2'][id*='store-search-modal-make-this-my-store']')))
make_this_my_store.click()   

点击商店暂时关闭按钮:

store_temporarily_closed = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[class*='btn btn-store-temporarily-closed btn-block btn-primary-2'][id='btnStoreCloseForEcommerce']')))
store_temporarily_closed.click()

请注意,与 xpath.
相比,css 选择器 总是好的 有关 xpath 与 css 选择器的更多信息,请参见 here

希望此信息helpful.Thanks !

您在测试中使用等待吗? 您的 xpath 变体必须有效。

尝试显式等待:

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

wait = WebDriverWait(driver, 5)
make_my_store = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(text(),'Make This My Store')]")))
make_my_store.click()

除非它在 ​​5 秒内找到 return 的元素,否则在抛出 TimeoutException 之前最多等待 5 秒。默认情况下,WebDriverWait 每 500 毫秒调用一次 ExpectedCondition,直到 return 成功。成功的 return 对于 ExpectedCondition 类型是 Boolean return true or not null return value for all other ExpectedCondition types.

尝试使用以下 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

希望对您有所帮助!

根据 HTML 您已将元素与文本共享为 将此设为我的商店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