gpt4 book ai didi

python - Selenium 在本地运行时可以找到元素,但在 Heroku 服务器上运行时找不到元素

转载 作者:行者123 更新时间:2023-12-04 07:22:10 24 4
gpt4 key购买 nike

当我在本地运行以下代码时,Selenium 能够找到所有元素并且一切正常:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from twilio.rest import Client

account_sid = ACCOUNT_SID
auth_token = AUTH_TOKEN
client = Client(account_sid, auth_token)

def runAmazonXBot():
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.amazon.com/Ticonderoga-Wood-Cased-Graphite-Pre-Sharpened-13818/dp/B002VL5IJO/ref=sr_1_5?dchild=1&keywords=pencil&qid=1610946344&sr=8-5")
driver.find_element_by_xpath("/html//input[@id='buy-now-button']").click()
driver.find_element_by_xpath("/html//input[@id='ap_email']").send_keys(EMAIL)
driver.find_element_by_xpath("//span[@id='continue']//input[@id='continue']").click()
driver.find_element_by_xpath("/html//input[@id='ap_password']").send_keys(PASSWORD)
driver.find_element_by_xpath("/html//input[@id='signInSubmit']").click()
driver.find_element_by_xpath("//span[@id='placeYourOrder']//input[@name='placeYourOrder1']").click()

runAmazonXBot()
message = client.messages \
.create(
body="Success!",
from_=PHONE_NUMBER_1,
to=PHONE_NUMBER_2
)
但是当我在 Heroku 服务器(涉及 Tweepy)中运行相同的代码时,如下所示:
import tweepy
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import os
from twilio.rest import Client
import traceback2 as traceback

chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)

consumer_key = CONSUMER_KEY
consumer_secret = CONSUMER_SECRET
access_token = ACCESS_TOKEN
access_token_secret = ACCESS_TOKEN_SECRET

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

def copAmazon():
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
driver.get("https://www.amazon.com/Ticonderoga-Wood-Cased-Graphite-Pre-Sharpened-13818/dp/B002VL5IJO/ref=sr_1_5?dchild=1&keywords=pencil&qid=1610946344&sr=8-5")
driver.find_element_by_xpath("/html//input[@id='buy-now-button']").click()
driver.find_element_by_xpath("/html//input[@id='ap_email']").send_keys(EMAIL)
driver.find_element_by_xpath("//span[@id='continue']//input[@id='continue']").click()
driver.find_element_by_xpath("/html//input[@id='ap_password']").send_keys(PASSWORD)
driver.find_element_by_xpath("/html//input[@id='signInSubmit']").click()
driver.find_element_by_xpath("//span[@id='placeYourOrder']//input[@name='placeYourOrder1']").click()

account_sid = ACCOUNT_SID
auth_token = AUTH_TOKEN
client = Client(account_sid, auth_token)
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.user.id_str == TWITTER_ID:
try:
copAmazon()
message = client.messages \
.create(
body='Success!',
from_=PHONE_NUMBER_1,
to=PHONE_NUMBER_2
)
except:
message = client.messages \
.create(
body='Fail! ' + traceback.format_exc(),
from_=PHONE_NUMBER_1,
to=PHONE_NUMBER_2
)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(follow=[TWITTER_ID])
我收到以下错误:“selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//span[@id='placeYourOrder ']//输入[@name='placeYourOrder1']"}
( session 信息: headless chrome=91.0.4472.164)“
基本上,当我在本地运行代码时,Selenium 可以很好地找到元素,但是当代码在 Heroku 服务器上运行时,Selenium 无法找到元素。为什么是这样?我很确定我已经正确设置了所有的构建包。此外,在上面的两个代码块中,ACCOUNT_SID、AUTH_TOKEN、PHONE_NUMBER_1、PHONE_NUMBER_2、CONSUMER_KEY、CONSUMER_SECRET、ACCESS_TOKEN、ACCESS_TOKEN_SECRET、EMAIL、PASSWORD 和 TWITTER_ID 都是正确的值(宏仅用于隐私目的)。有任何想法吗?谢谢!

最佳答案

所以我想出了为什么会这样。我刚刚注意到,每次在 Heroku 服务器上运行脚本时,我都会从亚马逊收到一条文本,说有“来自美国弗吉尼亚州的登录尝试”(这是 Heroku 服务器所在的位置),这意味着我必须先批准它,然后 Selenium ChromeDriver 才能继续。这就是为什么 ChromeDriver 在登录后无法立即找到“下订单”按钮的原因。我将尝试找到一种方法让亚马逊停止此验证过程,以便 ChromeDriver 在登录后不会停止,然后可以找到“下订单”按钮。

关于python - Selenium 在本地运行时可以找到元素,但在 Heroku 服务器上运行时找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68426173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com