Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 2.7中使用Selenium选择按钮_Python_Python 2.7_Selenium_Selenium Webdriver_Google Chrome Headless - Fatal编程技术网

在Python 2.7中使用Selenium选择按钮

在Python 2.7中使用Selenium选择按钮,python,python-2.7,selenium,selenium-webdriver,google-chrome-headless,Python,Python 2.7,Selenium,Selenium Webdriver,Google Chrome Headless,我正在抓取的一个站点最近更改了我使用的按钮ID。由于某种原因,我找不到新元素。我在选择一个按钮时浏览了多个站点(包括堆栈溢出),我尝试的都不管用。我几乎是Selenium的新手。以下是HTML摘录: <div class="info"> <h4 class="store-number"> Store Number: {{=storeId}}

我正在抓取的一个站点最近更改了我使用的按钮ID。由于某种原因,我找不到新元素。我在选择一个按钮时浏览了多个站点(包括堆栈溢出),我尝试的都不管用。我几乎是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>

结果如下:

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)

我错过了什么

更新:感谢到目前为止的建议。不幸的是,当我尝试多个变体时,由于找不到对象,我一直会遇到超时错误。我抓起driver.page查看源代码并查看:


让这成为我的商店

您可以尝试使用此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()
单击“存储临时关闭”按钮:

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(),它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码WebDriverWaitExpectedCondition相结合是实现这一目标的一种方法

有关显式等待的更多信息,请参见

正如你提到的

//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']  
代码中类似于:(用于单击将此设置为我的店铺

单击“存储临时关闭”按钮:

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选择器的更多信息,请参见


希望这些信息能对你有所帮助。谢谢

您可以尝试使用这个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()
单击“存储临时关闭”按钮:

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(),它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码WebDriverWaitExpectedCondition相结合是实现这一目标的一种方法

有关显式等待的更多信息,请参见

正如你提到的

//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']  
代码中类似于:(用于单击将此设置为我的店铺

单击“存储临时关闭”按钮:

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选择器的更多信息,请参见

希望这些信息能对你有所帮助。谢谢

您是否在测试中使用等待? 您的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类型,成功返回的值为布尔返回true或非null。测试中是否使用等待? 您的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类型的成功返回为布尔值,其他所有ExpectedCondition类型的返回值为true或not null。

尝试使用以下xpath:

button = browser.find_element_by_xpath("//button[@id='store-search-modal-make-this-my-store-{{=storeId}}']")
button.click() 
现在,如果这不起作用,那么驾驶员可能需要等待几秒钟,然后才能查找元件。在这种情况下,我建议使用显式等待。有关显式等待的更多信息,请参见此处=

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

    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

希望这有帮助

尝试使用以下xpath:

button = browser.find_element_by_xpath("//button[@id='store-search-modal-make-this-my-store-{{=storeId}}']")
button.click() 
现在,如果这不起作用,那么驾驶员可能需要等待几秒钟,然后才能查找元件。在这种情况下,我建议使用显式等待。有关显式等待的更多信息,请参见此处=

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

    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,您已将元素与文本共享为使此My Store是一个元素,因此要在所需元素上调用
click()
,您必须诱导WebDriverWait,使该元素成为clickab