如何通过Selenium将文本发送到在Facebook上创建帐户的元素

时间:2018-12-14 10:19:59

标签: python facebook selenium selenium-webdriver webdriver

from selenium import webdriver

driver = webdriver.Chrome("/home/rootlente/Desktop/Driver For Selenium Chrome/chromedriver_linux64/chromedriver")
driver.get("http://facebook.com")
driver.find_element_by_name("firstname").send_keys("Ilia")
driver.find_element_by_name("lastname").send_keys("james")
driver.find_element_by_name("reg_email__").send_keys("mail@mail.com")
driver.find_element_by_name("reg_email_confirmation__").send_keys("mail@mail.com")
driver.find_element_by_name("reg_passwd__").send_keys("password")
driver.find_element_by_css_selector("input[type='radio'][value='"sex"']")["2"].click()

2 个答案:

答案 0 :(得分:0)

要将字符序列发送到在 Facebook 主页上创建帐户的元素,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.facebook.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputtext[name='firstname']"))).send_keys("Ilia")
    driver.find_element_by_css_selector("input.inputtext[name='lastname']").send_keys("james")
    driver.find_element_by_css_selector("input.inputtext[name='reg_email__']").send_keys("mail@mail.com") 
    driver.find_element_by_css_selector("input.inputtext[name='reg_email_confirmation__']").send_keys("mail@mail.com") 
    driver.find_element_by_css_selector("input.inputtext[name='reg_passwd__']").send_keys("password")
    driver.find_element_by_xpath("//label[contains(@for, 'u_0_') and text()='Male']//preceding::input[1]").click()
    
  • 浏览器快照

facebook_registration

答案 1 :(得分:0)

当我检查页面时,它是name="sex",而value"2"(对于Male)。您有value="sex"

更改:

driver.find_element_by_css_selector("input[type='radio'][value='"sex"']")["2"].click()

选择男性:

driver.find_element_by_css_selector("input[type='radio'][name='sex'][value='2']").click()

driver.find_elements_by_css_selector("input[type='radio'][name='sex']")[1].click()



选择女性:

driver.find_element_by_css_selector("input[type='radio'][name='sex'][value='1']").click()

driver.find_elements_by_css_selector("input[type='radio'][name='sex']")[0].click()

编辑:我喜欢安德森(Andersson)的以下注释,他的代码比我的代码要好(在我看来),所以我添加了它。所以给你选择