我正在抓取的网站最近更改了我正在使用的按钮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 &&
model.store.closeForEcommerce !== 'True'" ng-click="model.makeMyStore();">
Make This My Store
</button>
我尝试使用XPATH,使用“ make-this-my-store”作为按钮ID,并使用“ btn btn-block btn-primary-2”作为CSS类,寻找“ Make This My Store”。全部给找不到对象的错误。
答案 0 :(得分:2)
您可以尝试使用 xpath 并可以使用显式等待:
要点击“ 创建我的商店” 按钮:
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']
在代码中类似:(用于单击将其保存在我的商店中)
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 vs css选择器的更多信息,请参见here
希望此信息会有所帮助。谢谢!
答案 1 :(得分:0)
您是否在测试中使用等待? 您的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秒钟,然后抛出TimeoutException,除非它发现要在5秒钟内返回的元素。默认情况下,WebDriverWait每500毫秒调用ExpectedCondition,直到成功返回。对于ExpectedCondition类型,成功返回是Boolean返回true,对于所有其他ExpectedCondition类型,返回值都是非null。
答案 2 :(得分:0)
尝试使用以下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
希望这会有所帮助!
答案 3 :(得分:0)
根据 HTML ,您已将元素与文本共享为“创建此我的商店” 是Angular元素,因此调用\n\n
在所需元素上,您必须诱使 WebDriverWait 使元素可点击,并且您可以使用以下解决方案:
click()
注意:您必须添加以下导入:
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()