- selenium python javascript单击按钮

  显示原文与译文双语对照的内容

我的selenium 脚本有问题。 我想webscrape上运行的一个网站。 我在网上收集了很多信息但是我找不到答案。 我还提交了一个HTML代码的printscreen,这是我在这篇文章中的一个例子。 基本上:我想在加载网站时点击接受按钮,但是我不知道如何做这个。

在不同网站上搜索解决方案。

你们能帮我解释一下我的脚本,我一直在尝试和测试,但我不能解决。 谢谢你。

代码:


from config import keys


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


import time



def order(k):



 chrome_path = 


 r"C:UsersltewoPycharmProjectslivebettingchromedriver.exe"


 driver = webdriver.Chrome(chrome_path)


 driver.get(keys['url'])


 xpath_button_accept ="//div[@class='cookieButtons']//a[@class='button 


 accept']"


 button_accept = driver.find_element_by_xpath(xpath_button_accept)


 xpath_button_accept.click()



if __name__ == '__main__':


 order(keys)



时间:

尝试这个:


#update


driver.implicity_wait(10)



xpath_button_accept ="//div[@class='cookieButtons']//a[@class='button accept']"



button_accept = your_browser.find_element_by_xpath(xpath_button_accept)


button_accept.click()




from config import keys


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


import time



def order(k):



chrome_path = r"C:UsersltewoPycharmProjectslivebettingchromedriver.exe"


driver = webdriver.Chrome(chrome_path)


driver.get(keys['url'])


xpath_button_accept ="//div[@class='cookieButtons']//a[@class='button accept']"


WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 


'xpath_button_accept')))



button_accept = driver.find_element_by_xpath(xpath_button_accept)


button_accept.click()



if __name__ == '__main__':


 order(keys)



要在页面上加载该元素,应等待 ,因此使用:


WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, 'XpathOfMyElement')))



更新了你的代码:


from config import keys


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


import time



def order(keys):



chrome_path = r"C:UsersltewoPycharmProjectslivebettingchromedriver.exe"


driver = webdriver.Chrome(chrome_path)


driver.get(keys['url'])


xpath_button_accept ="//div[@class='cookieButtons']//a[@class='button accept']"


WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 


'xpath_button_accept')))



button_accept = driver.find_element_by_xpath(xpath_button_accept)


button_accept.click()



if __name__ == '__main__':


 order(keys)



我已经在你的网站上更新了代码。 但我还是有错误的。 我知道我在烦你但是我还在学习。


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


import time



def order():



chrome_path = r"C:UsersltewoPycharmProjectslivebettingchromedriver.exe"


driver = webdriver.Chrome(chrome_path)


driver.get('https://saucelabs.com/resources/articles/the-selenium-click-command')


xpath_button_accept = ("/html/body/div[1]/div/a")


WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 


'xpath_button_accept')))



button_accept = driver.find_element_by_xpath(xpath_button_accept)


button_accept.click()



if __name__ == '__main__':


 order()



...