Selenium twitter无法获取用户名和密码

时间:2016-01-19 03:09:07

标签: python selenium twitter

以下代码是我的程序的一部分,我试图获取excel文件中给出的screen_names的关注者。之前我的代码工作正常,但现在每次在chrome上加载twitter页面后都会卡住。它不会在浏览器中输入用户名和密码。请指导我以下代码中的错误。

    *driver = webdriver.Chrome('C:/name/sport/chromedriver')
        driver.get("https://twitter.com/download?logged_out=1&lang=en")
        time.sleep(6) 
        driver.find_element_by_xpath("//*[@id='signin-link']").click()
        time.sleep(3)
        pagecount=driver.find_element_by_xpath("//*[@id='signin-email']").send_keys('mygmail')
        pagecount=driver.find_element_by_xpath("//*[@id='signin-password']").send_keys('mypassword')
        time.sleep(3)
        driver.find_element_by_xpath("//*[@id='signin-dropdown']/div[3]/form/input[1]").click()*


TypeError                                 Traceback (most recent call last)
<ipython-input-1-d3a52c563a6e> in <module>()
     18 driver.find_element_by_xpath("//*[@id='signin-link']").click()
     19 time.sleep(3)
---> 20 pagecount=driver.find_element_by_xpath("//*[@id='email']").send_keys('architgarg1515@gmail.com')
     21 pagecount=driver.find_element_by_xpath("//*[@id='Password']").send_keys('@Kaminibruno05')
     22 time.sleep(3)

C:\Users\khushi.tiwari\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    256             driver.find_element_by_xpath('//div/td[1]')
    257         """
--> 258         return self.find_element(by=By.XPATH, value=xpath)
    259 
    260     def find_elements_by_xpath(self, xpath):

C:\Users\khushi.tiwari\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    710                 value = '[name="%s"]' % value
    711         return self.execute(Command.FIND_ELEMENT,
--> 712                              {'using': by, 'value': value})['value']
    713 
    714     def find_elements(self, by=By.ID, value=None):

C:\Users\khushi.tiwari\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    199         response = self.command_executor.execute(driver_command, params)
    200         if response:
--> 201             self.error_handler.check_response(response)
    202             response['value'] = self._unwrap_value(
    203                 response.get('value', None))

C:\Users\khushi.tiwari\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    162 
    163         screen = None
--> 164         if 'screen' in value:
    165             screen = value['screen']
    166 

TypeError: argument of type 'NoneType' is not iterable

2 个答案:

答案 0 :(得分:1)

我查看https://twitter.com/download?logged_out=1&lang=en页面上的登录表单,我看不到包含@id='signin-email'@id='signin-password'的元素。相反,我将登录元素视为

<input type="text" class="text-input email-input js-signin-email" name="session[username_or_email]" autocomplete="username" placeholder="Phone, email or username">

和密码为

<input type="password" class="text-input" name="session[password]" placeholder="Password" autocomplete="current-password">

所以这意味着你的xpath是错误的。尝试

 pagecount=driver.find_element_by_xpath("//input[contains(@name,'username')]").send_keys('mygmail')
 pagecount=driver.find_element_by_xpath("//input[contains(@name,'password')]").send_keys('mypassword')

同样在异常转储中,我认为您忘记更换真实用户名/密码,因此您可能需要更改密码。

答案 1 :(得分:0)

我理解我回答这个问题为时已晚。.另一个简单的解决方案是仅使用类名中更相关的部分作为用户名和密码。

示例:

model = Autoencoder()
rec = model(x)
loss_1 = mse_loss(rec, x)
loss_2 = l1_loss(rec, x)

opt.zero_grad()
loss_1.backward(retain_graph=True)
loss_2.backward()
opt.step()

对我有用。