python - 硒的自定义命令循环?
问题描述
我想创建一个命令循环以向下一列复选框并让 selenium webdriver 检查 10,然后向下滚动并继续进行一定数量的代表。
我已经能够编写第一个复选框单击和之后向下滚动的代码。我需要能够为下一个复选框创建一个 XPATH 字符串,并制作一个循环来进行一定数量的轮次。
到目前为止,这是我的代码
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#u_0_u > div > div > div > div:nth-child(3) > div > "
"div:nth-child(2) > table > tbody > tr:nth-child(1) "
"> ""td:nth-child(1) > label > span")))
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
time.sleep(10)
driver.execute_script("window.scrollTo(0,400)")
这是 XPATH,其中的变量需要以粗体顺序更改
//*[@id="u_0_u"]/div/div/div/div[3]/div/div[2]/table/tbody/**tr[1]**/td[1]/label/span
这是前 3 行的 HTML。
<tr class=""><td class=" _100u"><label class="_55sg _kv1"><input name="select_single_row_checkbox[]" type="checkbox" value="100003109554020"><span class="_66ul"></span></label></td><td class=" _100u"><div direction="left" class="clearfix"><div class="_ohe lfloat"><img src="https://graph.facebook.com/100003109554020/picture?width=32&height=32" class="img _8o _8r img" alt=""></div><div class=""><div class="_42ef _8u"><div><a data-hovercard="/ajax/hovercard/user.php?id=100003109554020" class="_3cb8" href="/100003109554020">Caio Augusto</a></div></div></div></div></td><td class=" _100u"><abbr aria-label="4 days ago" class="livetimestamp" data-utime="1587140162">04/17/20</abbr></td></tr>
<tr class=""><td class=" _100u"><label class="_55sg _kv1"><input name="select_single_row_checkbox[]" type="checkbox" value="100033497255249"><span class="_66ul"></span></label></td><td class=" _100u"><div direction="left" class="clearfix"><div class="_ohe lfloat"><img src="https://graph.facebook.com/100033497255249/picture?width=32&height=32" class="img _8o _8r img" alt=""></div><div class=""><div class="_42ef _8u"><div><a data-hovercard="/ajax/hovercard/user.php?id=100033497255249" class="_3cb8" href="/100033497255249">Cam Ailiff</a></div></div></div></div></td><td class=" _100u"><abbr aria-label="a week ago" class="livetimestamp" data-utime="1586814525">04/13/20</abbr></td></tr>
<tr class=""><td class=" _100u"><label class="_55sg _kv1"><input name="select_single_row_checkbox[]" type="checkbox" value="691216152"><span class="_66ul"></span></label></td><td class=" _100u"><div direction="left" class="clearfix"><div class="_ohe lfloat"><img src="https://graph.facebook.com/691216152/picture?width=32&height=32" class="img _8o _8r img" alt=""></div><div class=""><div class="_42ef _8u"><div><a data-hovercard="/ajax/hovercard/user.php?id=691216152" class="_3cb8" href="/691216152">Dave Anderson</a></div></div></div></div></td><td class=" _100u"><abbr aria-label="7 weeks ago" class="livetimestamp" data-utime="1582826382">02/27/20</abbr></td></tr>
请告知如何使这成为可能。
解决方案
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")
option.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 2})
driver = webdriver.Chrome(options=option, executable_path='PATH TO CHROME DRIVER')
driver.get("https://www.facebook.com/CHANGE_PAGE_USERNAME_HERE/settings/?tab=people_and_other_pages&ref=page_edit")
time.sleep(2)
driver.find_element_by_xpath("//input[@id='email']").send_keys("EMAIL")
driver.find_element_by_xpath("//input[@id='pass']").send_keys("PASS")
time.sleep(2)
driver.find_element_by_xpath("//input[starts-with(@id, 'u_0_')][@value='Log In']").click()
time.sleep(5)
driver.refresh()
#time.sleep(5) #turn on to sort through banned
#driver.find_element_by_xpath("//*[@id='u_0_u']/div/div/div/div[3]/div/div[1]/div[1]/a/span[1]").click()
#time.sleep(5)
#driver.find_element_by_xpath("//*[@id='js_s']/div/ul/li[4]/a").click()
#time.sleep(9)
f = 1
whitelist = 5000
while f <= whitelist:
driver.execute_script('window.scrollBy(0,5000)')
time.sleep(1)
f += 1
continue
wait = WebDriverWait(driver, 10)
driver.execute_script("scrollBy(0,0);")
toggle_all = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_0_u']/div/div/div/div[3]/div/div[2]/table/"
"thead/tr/th[1]/label/span")))
actions = ActionChains(driver)
actions.move_to_element(toggle_all).perform()
driver.execute_script("arguments[0].click();", toggle_all)
wait = WebDriverWait(driver, float(.0001))
i = 1
targets = 100 # this is where you change how many checkboxes will be selected
while i <= targets:
xpath_string = "//*[@id='u_0_u']/div/div/div/div[3]/div/div[2]/table/tbody/tr[" + str(i) + "]/td[1]/label/span"
element = wait.until(EC.element_to_be_clickable((By.XPATH, xpath_string)))
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
i += 1
continue
推荐阅读
- django - /admin 处的 ModuleNotFoundError 没有名为“winsound”的模块
- python - scrapy爬虫的请求被阻塞。使用requests库的请求可以正常访问。什么原因?
- node.js - 如何使用 Dialogflow 执行 Firebase 身份验证
- javascript - Svelte/Sapper - 运行 __sapper__/build 找不到模块
- python - 相对于文本文件中另一列中的另一个值,逐个读取列中的每个值
- kubernetes - 我可以使用 client-go 来获取 kubelet 信息,例如“ps -ef | grep kubelet”吗?
- javascript - ES6中过滤Javascript数组中重复项的最优雅方法是什么
- xamarin.forms - 如何使用 Plugin.mediamanager.forms 播放 xamarin.forms 中的视频列表
- c# - mvc 表单不发布到控制器
- python - 如何使用 python 将以下代码中所有生成的图像保存到 Zip 文件中?