我正在https://www.rogers.com/web/totes/wireless/build-plan上抓网,当您在电话上单击并转到包含计划的不同排列的页面时,您可以移动滑块以选择最适合您的一个。现在,我希望在每次移动滑块后抓取数据。但是,当我使用Selenium时,条形图不是从左侧开始,而是任意地在滑块上的点之间跳来跳去。对于某些手机,经过滑块的第一点后,我的代码遇到了错误。有时,滑条上的某些点会全部丢失或单击两次。
我尝试了不同的CSS。选择器(但有些限制钢筋长度)会错过所有不同的计划类型。我尝试打印webElement信息,以查看其是否两次选择了某些内容,但每次看起来都不同。因此,它不能始终如一地或根据我的需要通过滑块
#collects all different points in slider
planSlider = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.desSliderWrapper div')))
print(len(planSlider))
#runs through slider points
for plan in planSlider:
time.sleep(2)
print(plan)
plan.click()
我希望能够依次遍历从滑块左侧到右侧的点,无论它具有3个点还是5个点,等等。
答案 0 :(得分:1)
这是选择计划然后获取详细信息的逻辑。
plans = driver.find_elements_by_css_selector("div.card-slider li")
for planNum in range(1,len(plans)+1):
print("------------------------------------------------------------------")
plan = driver.find_element_by_css_selector("div.card-slider li:nth-of-type("+str(planNum) + ")")
plan.click()
print(plan.text)
print(driver.find_element_by_xpath("//div[strong['plans from:']]").text)
以下是输出:
已编辑:
这是将选择计划的脚本。
# get the plan selection span
planSelector = driver.find_element_by_xpath("//span[@class='ui-slider-handle ui-state-default ui-corner-all']")
# set the mouse position based on the number of plans
numberOfPlans = len(driver.find_elements_by_xpath("(//div[starts-with(@class,'sectionTable tabCount-')])[1]//div[starts-with(@class,'sectionCol')]"))
for selectPlan in range(1,5): #<== just looping to make sure it's working for all the plans (remove this and uncomment "selectPlan" variable in the below line)
# specify the plan that you want to select
# selectPlan = 4 #<======== change this number based on your interested plan number
percentage = round((100/(numberOfPlans-1))*(selectPlan-1),4)
print(percentage)
#select plan
# driver.execute_script("arguments[0].dispatchEvent(new MouseEvent('mousedown', {'bubbles': true,'cancelable': true}))",planSelector)
driver.execute_script("arguments[0].setAttribute('style','left: " + str(percentage) + "%;')",planSelector)
time.sleep(1)
# now trigger the mouse move event which will tiger the change in the price
# this will make sure js attached to the element in DOM will trigger
driver.execute_script("arguments[0].dispatchEvent(new MouseEvent('mousedown', {'bubbles': true,'cancelable': true}))",planSelector)
# time.sleep(1)
driver.execute_script("arguments[0].dispatchEvent(new MouseEvent('mousemove', {'bubbles': true,'cancelable': true}))",planSelector)
driver.execute_script("arguments[0].dispatchEvent(new MouseEvent('mouseup', {'bubbles': true,'cancelable': true}))",planSelector)
time.sleep(1)
selectedPlan = driver.find_element_by_class_name("selectedPlanRed").text
print ("Selected Plan:-" + selectedPlan)
time.sleep(3)
屏幕截图不足,无法容纳大小限制: