Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 硒找不到元素_Python_Selenium_Xpath - Fatal编程技术网

Python 硒找不到元素

Python 硒找不到元素,python,selenium,xpath,Python,Selenium,Xpath,我正在尝试编写一个代码,使用selenium自动创建gmail。我试图查找的项目是以下url中输入的名字: 我使用了wait,但它返回TimeoutException错误。 还有,假设我想使用隐式等待,我该怎么做呢 谢谢 class BotCreator: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname

我正在尝试编写一个代码,使用selenium自动创建gmail。我试图查找的项目是以下url中输入的名字:

我使用了wait,但它返回TimeoutException错误。 还有,假设我想使用隐式等待,我该怎么做呢

谢谢

class BotCreator:

    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        self.driver = webdriver.Firefox(executable_path=r'A:\Python Projects\The InstaBOT/geckodriver')

    def shutdown(self):
        self.driver.close

    def gmail_creator(self, n_bots):
        for n in range(n_bots):
            global email
            email = {}
            driver = self.driver
            wait = WebDriverWait(driver, 10)
            driver.get('https://www.google.com/intl/en-GB/gmail/about/')
            driver.find_element_by_xpath('//a[@title="Create an account"]').click()
            wait.until(ec.new_window_is_opened(driver.window_handles))
            after = driver.window_handles[1]
            driver.switch_to.window(after)
            element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@id="firstName"]')))
            element.send_keys('awsd')

        return email

gmail_t = BotCreator('John', 'Hoffinsky')
gmail_t.gmail_creator(1)


实际上,该页面上有4个元素具有相同的XPath。 尝试使用具有以下XPath的特定XPath:

(//a[@title="Create an account"])[1]
XPath返回4个带有Chrome扩展名ChroPath的元素

关于您关于隐性等待的问题: 您已经在使用隐式等待。隐式等待设置告诉Selenium在元素可用之前轮询指定的时间量。如果需要,您可以更改超时:

driver.implicitly_wait(15)

这可能会有帮助。

该页面上实际上有4个元素具有相同的XPath。 尝试使用具有以下XPath的特定XPath:

(//a[@title="Create an account"])[1]
XPath返回4个带有Chrome扩展名ChroPath的元素

关于您关于隐性等待的问题: 您已经在使用隐式等待。隐式等待设置告诉Selenium在元素可用之前轮询指定的时间量。如果需要,您可以更改超时:

driver.implicitly_wait(15)

这可能会有帮助。

使用ID总是比使用XPath/CSS好

范例

driver.find_element_by_id("FirstName").send_keys("email")

使用ID总是比使用XPath/CSS好

范例

driver.find_element_by_id("FirstName").send_keys("email")

单击“创建帐户”按钮后,将打开一个新窗口,显示用户详细信息。您需要切换到该窗口以访问元素。请尝试下面的代码

wait = WebDriverWait(driver, 10)
driver.get('https://www.google.com/intl/en-GB/gmail/about/')
driver.find_element_by_xpath('//a[@title="Create an account"]').click()
wait.until(ec.new_window_is_opened(driver.window_handles))
after=driver.window_handles[1]
driver.switch_to.window(after)
element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@id="firstName"]')))
element.send_keys('awsd')
浏览器快照:


单击“创建帐户”按钮后,将打开一个新窗口,显示用户详细信息。您需要切换到该窗口以访问元素。请尝试下面的代码

wait = WebDriverWait(driver, 10)
driver.get('https://www.google.com/intl/en-GB/gmail/about/')
driver.find_element_by_xpath('//a[@title="Create an account"]').click()
wait.until(ec.new_window_is_opened(driver.window_handles))
after=driver.window_handles[1]
driver.switch_to.window(after)
element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@id="firstName"]')))
element.send_keys('awsd')
浏览器快照:



谢谢你的回答。我试图输入我的名字,而不是创建一个帐户。我已经点击了CreateanAccount,我的代码中的wait,webdriverwait;它与隐式wait的区别是什么隐式等待不需要任何额外的代码。它总是被使用。代码中的显式等待是额外的等待。你用它来做一个特定的动作,我不知道。所以我只是改变了等待的时间。但是我仍然找不到元素。我还将code.element=wait.untelec.element更新为可点击@KunduK建议我删除for循环,现在它可以工作了,但我不知道为什么!谢谢你的回答。我试图输入我的名字,而不是创建一个帐户。我已经点击了CreateanAccount,我的代码中的wait,webdriverwait;它与隐式wait的区别是什么隐式等待不需要任何额外的代码。它总是被使用。代码中的显式等待是额外的等待。你用它来做一个特定的动作,我不知道。所以我只是改变了等待的时间。但是我仍然找不到元素。我还将code.element=wait.untelec.element更新为可点击@KunduK建议我删除for循环,现在它可以工作了,但我不知道为什么!你们能确认一下,一旦你们点击CreateCount,你们是在同一个页面上还是打开了一个新窗口?它会在firefox中打开一个新的点击。因为新标签是打开的,我想这不是问题所在。是吗?是的,您需要切换到该窗口才能访问该元素。我已经添加了代码。您能否确认,一旦单击“创建帐户”,您是否在同一页面上,或者它会打开一个新窗口?它会在firefox中打开一个新的点击。因为新标签是打开的,我想这不是问题所在。是吗?是的,您需要切换到该窗口才能访问元素。我已添加了代码。此链接可能会帮助您,此链接可能会帮助您,仍然给出错误:selenium.common.exceptions.TimeoutException:Message:是的,我做了。我真的很困惑too@Johnsmith:for循环是什么?我使用该循环可以创建多个gmail。n_bots是Gmail的数量必须首先使用给定代码创建单个帐户并检查是否有错误?仍然会给出错误:selenium.common.exceptions.TimeoutException:Message:是的,我有。我真的很困惑too@Johnsmith:for循环是什么?我使用该循环可以创建多个gmail。n_bots是Gmail的数量必须先用给定的代码创建一个帐户,然后检查是否有错误?