2017-04-05 127 views
0

我想写一个脚本来自动登录到ebay使用python中的硒模块。脚本自动登录到ebay使用硒python

我可以在Firefox中启动登录页面。这里的代码:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 

firefox_capabilities = DesiredCapabilities.FIREFOX 
firefox_capabilities['marionette'] = True 
firefox_capabilities['binary'] = '/usr/bin/firefox' 

driver = webdriver.Firefox(capabilities=firefox_capabilities) 
driver.get("https://signin.ebay.in/ws/eBayISAPI.dll?SignIn&ru=http%3A%2F%2Fwww.ebay.in%2F") 

search_field = driver.find_element("Email or username") 
search_field.clear() 
search_field.send_keys("My username") 

但这似乎不工作。我不能使用

​​3210

因为id保持动态变化。

<input size="40" maxlength="64" name="147630419" id="147630419" autocapitalize="off" autocorrect="off" placeholder="Email or username" class="fld" type="text"> 

这就是电子邮件占位符的样子。密码的占位符也类似。 “名称”和“ID”动态变化。

我该如何解决这个问题?

+0

尝试的XPath' “” 提出了一个(//输入[@ placeholder ='Email or username'])[2]“' –

回答

1

我不太确定driver.find_element()的用法,但我认为在driver.find_element()中,您需要传递用户名字段的位置(例如:xPath),而不是字符串格式的输入字段的占位符。

用户名字段的XPath是"(//input[@placeholder='Email or username'])[2]",所以尝试:

driver.find_element_by_xpath("(//input[@placeholder='Email or username'])[2]")

UPDATE

密码字段的XPath是"(//input[@placeholder='Password'])[1]"

+0

您好@Jayesh,谢谢。它已经为我工作了。脚本现在可以在字段中输入用户名了,我只是想知道[ 2]意味着,我也尝试做同样的密码提交,因为这两个元素看起来相同,但它不起作用。 –

+0

@VivekBhageria xPath'//输入[@placehol der ='Email or username']'返回数组中的2个元素。由于数组的第二个元素是我们想要的,所以我做了[2],这基本上意味着选择索引2处的元素(索引从1开始) –

+0

@VivekBhageria我更新了我的答案以包含密码字段的xPath。 –

1

因为ID是动态变化去的XPath 。试试这段代码。它的工作原理

driverInstance.find_element_by_xpath(".//*[@id='pri_signin']/div[4]/span[2]/input").send_keys("Hello") 
1

您使用find_element()方法不正确,你应该通过2个参数:byvalue

你可以试试下面的代码行:

driver.find_element("css", "input:not(#userid)[placeholder='Email or username']").send_keys("My username") 

,但最好使用适当的方法find_element_by_...()像通过@Jayesh Doolani(+1)

+0

不应该是'By.CSS_SELECTOR'而不是''css“' –

+0

如果先前导入了selenium.webdriver.common.by.By,它可能是'By.CSS_SELECTOR'。 '“css”'也是可以接受的 – Andersson

+0

嗨安德森。我试过这一行代码,但它会抛出selenium.common.exceptions.WebDriverException:Message:Unknown locator strategy css'错误。 Jayesh提出的那个工作。我现在需要让它在密码字段中工作。 –