python - 对 Python 非常陌生,需要 Selenium Webdriver 来检查复选框
问题描述
这是我的第一篇文章。我正在努力学习基本的编程,以便能够更好地与为我公司工作的开发人员在一个完全独立的项目上进行交流。
我正在对公司的 facebook 页面进行一些内务处理,并希望使用 Python 编码的 Selenium webdriver 运行一个脚本,以帮助我快速检查 facebook“人民”页面上的多个复选框。
除了能够定位和检查所需的框外,我已经深入了解了脚本。
请告知我如何定位和选择复选框,然后向下行以连续为指定数量的框执行此操作。
这是我所在的位置:
import time
import selenium.webdriver.support.wait
from selenium import webdriver
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.common.action_chains import ActionChains
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", {
"profile.default_content_setting_values.notifications": 2
})
driver = webdriver.Chrome(options=option, executable_path='C:\\webdrivers\\chromedriver')
driver.get("https://www.facebook.com/eddiejagmedia/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()
element: object = selenium.webdriver.support.wait.WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "ContentPlaceHolder1_wucSignInStep2_chkTC")))
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
这是复选框的 HTML 代码,但不确定我应该抓取哪个父级。
<label class="_55sg _kv1"><input name="select_single_row_checkbox[]" type="checkbox" value="100003109554020"><span class="_66ul"></span></label>
最后,这是一张有助于想象范围的图片。
非常感谢,爱德华多
解决方案
请尝试以下解决方案:
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_wucSignInStep2_chkTC")))
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
注意:请在您的解决方案中添加以下导入
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
推荐阅读
- sql - 最大函数返回多个值 [SQL]
- spring - Spring 4、MVC 和 JPA 集成
- applescript - AppleScript: "use framework" errors out in "Expected end of line, etc. but found identifier."
- javascript - JS Regex solution to turn reddit style links into html?
- animation - 当我使用带有 blit = True 的 matplotlib 动画函数时,如何保留最后一帧
- javascript - 如何制作一个通用的正则表达式来验证通过 JS 输入的 UTF-8 特殊字符?
- macos - gmp error: cannot determine how to define a 32-bit word
- javascript - 检查复选框的状态
- c - What would happen if I call getpeername() and getsockname() on server socket at different positions?
- ios - SpriteKit 场景变得无响应