Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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
Click()函数在python/selenium中不起作用_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Click()函数在python/selenium中不起作用

Click()函数在python/selenium中不起作用,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,正如你所看到的,我开始在SNKR上编写一个机器人来处理运动鞋。我已经编写了一些无用的代码,但我们不在乎。我想让机器人点击登录按钮,但在我运行代码的最后,它会打开Chrome浏览器,然后打开nike网站,但没有点击按钮,我收到以下错误消息: Traceback (most recent call last): File "C:/Users/xxx/xxx/xxx/xxx/xxx/SNKRS_bot/snkrs bot.py", line 11, in <module&

正如你所看到的,我开始在SNKR上编写一个机器人来处理运动鞋。我已经编写了一些无用的代码,但我们不在乎。我想让机器人点击登录按钮,但在我运行代码的最后,它会打开Chrome浏览器,然后打开nike网站,但没有点击按钮,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/xxx/xxx/xxx/xxx/xxx/SNKRS_bot/snkrs bot.py", line 11, in <module>
    loginBtn = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button").click()
AttributeError: 'list' object has no attribute 'click'

非常感谢

请参阅我对您发布的问题的评论。我最终找到了你的按钮,但如果不使用JavaScript,它是不可点击的:

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
try:
    driver.implicitly_wait(6) # wait up to 6 seconds to find an element
    driver.get("https://www.nike.com/ch/fr/launch?s=upcoming")
    
    loginBtns = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button")
    if loginBtns:
        #loginBtns[0].click()
        driver.execute_script('arguments[0].click()', loginBtns[0])
    else:
        print('Could not find login button.')
finally:
    driver.quit()

我根本找不到您要查找的xpath。此外,您正在调用
find\u elements\u by\u xpath
,它返回一个列表。假设它找到一个或多个满足xpath的元素,则不能将列表传递给
click
函数;您必须传递列表中的一个元素。与其调用
sleep
,不如在一开始就调用
driver.implicit_wait(6)
。然后,驱动程序将等待长达6秒的时间来查找元素,但如果更快地找到元素,则会更快地返回。这是否回答了您的问题?见:
from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
try:
    driver.implicitly_wait(6) # wait up to 6 seconds to find an element
    driver.get("https://www.nike.com/ch/fr/launch?s=upcoming")
    
    loginBtns = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button")
    if loginBtns:
        #loginBtns[0].click()
        driver.execute_script('arguments[0].click()', loginBtns[0])
    else:
        print('Could not find login button.')
finally:
    driver.quit()