我正在尝试编写脚本来自动化下载instagram故事的过程,但是尝试登录时我已经失败了。
我正在Pycharm
中编写代码。我只是尝试了通常的方法来解决任何问题。首先,通过在控制台中键入命令来解决它,如果它可以将在控制台内部起作用的命令写在脚本中,则可以解决该问题。但这是问题所在。在 python控制台中正常运行的函数在脚本中失败。
我注意到我的selenium
已经过时,但是升级它并没有帮助以太。我还创建了一个新项目来测试天气有所不同的项目,
我还尝试跳过脚本中的第一步,只是打开重定向到的URL。但是第二条命令也失败了。
当我创建一个新变量来存储driver.find_element_by_link_text()
的输出时,它将返回一个空列表。这使我相信硒无法以某种方式搜索页面内容。
我也在Chrome和Safari上尝试过相同的方法。这也行不通。
代码如下:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
在控制台中,这些命令按上述方式工作, 这是我输入到控制台中的内容:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
driver.find_element_by_link_text("Melde dich an.").click()
#if it failed here would be an error message
element = driver.find_element_by_name("username")
使用脚本,错误消息是这样的:
回溯(最近通话最近):文件 “ / Users / alisot2000 / PycharmProjects / Instagram下载器/venv/Main.py”, 第6行 driver.find_element_by_link_text(“ Melde dich an。”)。click()文件“ / Users / alisot2000 / PycharmProjects / Instagram downloader / venv / lib / python3.7 / site-packages / selenium / webdriver / remote / webdriver.py”, 第428行,在find_element_by_link_text中 返回self.find_element(by = By.LINK_TEXT,value = link_text)文件“ / Users / alisot2000 / PycharmProjects / Instagram downloader / venv / lib / python3.7 / site-packages / selenium / webdriver / remote / webdriver.py”, 第978行,在find_element中 '值':值})['值']文件“ / Users / alisot2000 / PycharmProjects / Instagram downloader / venv / lib / python3.7 / site-packages / selenium / webdriver / remote / webdriver.py”, 第321行,在执行中 self.error_handler.check_response(响应)文件“ / Users / alisot2000 / PycharmProjects / Instagram 下载器/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py”, 第242行,在check_response中 引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:无法 定位元素:Melde dich an。
答案 0 :(得分:0)
def ClickElementByName(name,driver):
while True:
try:
driver.find_element_by_name(name).click()
break
except:
sleep(1)
pass
Too long to wait the website run.
Replace ClickElementByName("username", driver)
答案 1 :(得分:0)
对于大多数自动化任务,网页的加载时间会根据机器的处理能力以及您的互联网连接强度而有所不同。
为解决此问题,我们可以使用从硒导入库等待。 下面是一个示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
在大多数情况下,Selenium在运行自动化脚本时将使用您的语言环境,但在您可能想要另一种语言的情况下,此处是FireFox的示例代码。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
# switch out 'de' with another two character language code
profile.set_preference("intl.accept_languages",'de')
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
driver.close()
以下是您的代码和更改后的代码的区别:https://www.diffchecker.com/G0WWB4Ry
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# these two imports are for setting up firefox driver and options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# import these three lines below if you are having synchronization issues
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
profile = webdriver.FirefoxProfile()
# here is where you need to set your language explicitly if its defaulting to an undesired language
# just replace the second parameter with your 2 character language code
# this line is not needed if your desired language is locale
profile.set_preference("intl.accept_languages",'de')
# throw in your path here <insert_your_gecko_driver_path_here>
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
# added these two lines below to solve synchronization issue
# element wasnt clickable until page finished loading
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Melde dich an.")))
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
答案 2 :(得分:0)
driver.find_element_by_xpath('// input [@ name =“ username”]')。send_keys(“ HereIsTheUsername”) driver.find_element_by_xpath('// input [@ name =“ password”]')。send_keys(“ HereIsTheUsername”) driver.find_element_by_xpath('// div [text()=“登录”]')。click()