![](/img/trans.png)
[英]Python Selenium PhantomJS not clicking on same button that Firefox Webdriver is able to click
[英]clicking button with python/selenium/phantomjs
我想凑这个与Python 3.5 /硒/ phantomjs的组合。
有一个按钮可以加载更多优惠
<button data-behavior="result-paging" class="button button-text--centered">
Mehr laden
</button>
这个确切的按钮在HTML代码内两次,如果列出了所有优惠,则第一个按钮的外观如下,而第二个按钮保持不变:
<button data-behavior="result-paging" class="button button-text--centered is-hidden">
Mehr laden
</button>
要单击我尝试的第一个按钮:
while True:
try:
time.sleep(4)
btnElements = driver.find_elements_by_xpath("//button[@data-behavior='result-paging']")
for btnElement in btnElements:
btnElement.click()
# btnElement.send_keys("\n")
if i==11:
break
else:
i=i+1
except:
break
和
while True:
try:
time.sleep(4)
elements= driver.find_elements_by_xpath("//button[@data-behavior='result-paging']")
driver.execute_script("arguments[0].click();", elements[0])
if i==11:
break
else:
i=i+1
except:
break
循环只是为了快速测试而存在,因为脚本将永久运行。 使用展位方法时,不会加载任何新内容,因此无法进行点击。
有谁知道如何解决这个问题(执行点击并加载其他报价)?
编辑:
显然,此问题是由phantomjs引起的,因为我可以使用chromedriver运行相同的脚本。 不幸的是,我需要运行phantomjs。 有没有人经历过这种行为?
根据您的代码块, 定位策略已被用作...
find_elements_by_xpath("//button[@data-behavior='result-paging']")
...不会将带有文字的可见按钮唯一标识为梅尔 。
要将带有文字的可见按钮唯一标识为“ 满载” ,可以使用以下“ 定位器策略”之一 :
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC # lines of code WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-psa-scope=tarifflist] button.button.button-text--centered"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC # lines of code WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-psa-scope='tarifflist']//button[@class='button button-text--centered']"))).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.