任何帮助都非常感谢,提前感谢
以下是示例代码
driver = webdriver.Chrome()
driver.get("https://www.footpatrol.co.uk/footwear/274670-nmd_r2-pk.html")
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//form/fieldset/div[1]/div[2]/div/select/option[@value='181']"))).click()
size = driver.find_element_by_xpath("//form/fieldset/div[1]/div[2]/div/select/option[@value='181']")
#
size.click()
driver.find_element_by_id('add-to-basket').click()
获得以下错误
Traceback (most recent call last):
File "test12.py", line 13, in <module>
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//form/fieldset/div[1]/div[2]/div/select/option[@value='181']"))).click()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.12.5 x86_64)
答案 0 :(得分:0)
这就是你的脚本的问题
1,而不是单击可见的元素(大小),您试图选择大小(背景)元素。
可见的size元素是一个链接,我认为在后台调用dropdown html元素并选择大小。因此,如果您只是单击可见的大小元素(链接)而不是单击下拉列表,那么您可以查找。
使用firebug工具检查特定元素的html。
driver = webdriver.Chrome()
driver.get("https://www.footpatrol.co.uk/footwear/274670-nmd_r2-pk.html")
size = WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//ul[@class='attribute_value_list']//li/a[text()='8']")))
size.click()
driver.find_element_by_id('add-to-basket').click()
请注意,请了解如何构建 XPath 和 CSS 选择器以选择特定元素。你使用的xpath是非常日志的,即使大小上最小的东西会改变也容易破坏。
答案 1 :(得分:0)
以下是您的问题的答案:
您为<div class="container">
<div class="sidebarcontainer">
<div class="innersidebarcontainer">
<div class="sidebar">
</div>
</div>
</div>
<!--
-->
<div class="mainpage">
<div class="page">
<h1>page</h1>
</div>
</div>
</div>
<div class="footer"></div>
构建的xpath
指的是8
上的其他元素。我们需要为元素构造一个唯一的HTML DOM
。以下是点击xpath
然后点击8
的示例代码:
ADD TO BASKET
如果这回答你的问题,请告诉我。
答案 2 :(得分:0)
如果presence_of_element_located
不起作用,请尝试visibility_of_element_located
例如,代替:
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//form/fieldset/div[1]/div[2]/div/select/option[@value='181']"))).click()
尝试:
WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, "//form/fieldset/div[1]/div[2]/div/select/option[@value='181']"))).click()
答案 3 :(得分:-1)
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//form/fieldset/div[1]/div[2]/div/select/option[@value='181']"))).click()
有时.click()不起作用。 stackoverflow中有很多与此相关的问题,但没有人真正有效。有时帮助Javascript执行者。 随意在评论中提问
解决问题的第二种方法,试试这个:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('add-to-basket').setAttribute('visibility', 'true');");
driver.findElement(By.id("add-to-basket")).click();