首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium在本地运行时可以找到元素,但在Heroku服务器上运行时找不到元素

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

Stack Overflow用户
提问于 2021-07-18 13:05:06
回答 1查看 208关注 0票数 2

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

代码语言:javascript
复制
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 ),如下所示:

代码语言:javascript
复制
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'//input@name='placeYourOrder1'"} (会话信息: 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都是正确的值(宏仅用于保密目的)。有什么想法吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-07-25 07:23:09

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68426173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档