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
Python 单击函数';我不想研究投票的要素_Python_Selenium_Selenium Webdriver_Scroll_Webdriverwait - Fatal编程技术网

Python 单击函数';我不想研究投票的要素

Python 单击函数';我不想研究投票的要素,python,selenium,selenium-webdriver,scroll,webdriverwait,Python,Selenium,Selenium Webdriver,Scroll,Webdriverwait,我正在用Python做实验。我正试着点击评论下面的向上或向下投票按钮。我使用XPath来确定specyfic按钮。没有发生错误,但单击后计数器不会增加。我尝试了不同的网页,但结果是一样的 我的第一种方法是,我使用了find_element_by()函数,但之后我无法对返回的元素使用click()方法。现在我正在使用动作链 这是我的剧本 from selenium import webdriver from selenium.webdriver.support import expected_co

我正在用Python做实验。我正试着点击评论下面的向上或向下投票按钮。我使用XPath来确定specyfic按钮。没有发生错误,但单击后计数器不会增加。我尝试了不同的网页,但结果是一样的

我的第一种方法是,我使用了find_element_by()函数,但之后我无法对返回的元素使用click()方法。现在我正在使用动作链 这是我的剧本

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

import time

driver = webdriver.Firefox()

driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
driver.maximize_window()
wait = WebDriverWait(driver,30)
action = ActionChains(driver)

cookieButton = wait.until(EC.element_to_be_clickable((By.ID,"inforcwp-y")))
cookieButton.click()

time.sleep(5)

#wait.until(EC.visibility_of((By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[9]/div[2]/div/ul/li[20]/p[1]/span[4]/a[2]")))

element = driver.find_element(By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/ul/li[8]/p[1]/span[4]/a[2]")


element.location_once_scrolled_into_view

time.sleep(5)
action.double_click(element)

time.sleep(5)
driver.quit()
我希望在点击“投票手”后增加上/下投票数 请给mo一些建议,如何实现我的目标

向上投票图标上单击
,您需要诱导WebDriverWait才能单击元素,您可以使用以下方法:

  • 代码块:

    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
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument('disable-infobars')
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"inforcwp-y"))).click()
    driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//span[@class='headerUnderline' and contains(., 'Komentarze')]"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//ul[@id='commentsList']/li/p//span[@class='kf-rating']//a[@class='ratingUp']"))).click()
    

不确定为什么要对
向上投票/向下投票
使用自动化。。。。无论如何,您是否尝试过使用
元素。单击()
?错误是什么?网页本身似乎有问题。当我手动单击upvote时,有时计数器上升,有时页面滚动到顶部,有时计数器上升一个以上。你能检查一下它在其他网页上是否有效吗?很明显,你可能想了解一下。@supputuri 1它是用于教育目的的2没有方法click()@emilianov我在3个不同的页面上进行了检查。我注意到了和你一样的事实,还有一个。当我不使用selenium(remote mod)浏览器时,所有链接都可以正常工作。感谢您的回复是否需要滚动脚本?@pythonbenginer好问题,在我看来,滚动是必要的,因为所需元素最初是不可交互的,除非您在视口中滚动它们。先生,您能解释一下您在哪里找到“//ul[@id='commentsList']/li/p//span[@class='kf-rating']//a[@class='ratingUp']”XPATH?当我单击复制XPath时,我收到了这个/html/body/div[2]/section/div[2]/div[1]/div[1]/div/div[4]/div[2]/div/ul/li[12]/p[1]/span[4]/a[1]@pythonbengineer您已经使用了绝对XPath,这就是自动化工程师开始编写XPath的方式。拍拍你自己,你在正确的轨道上。渐渐地,您应该开始自己编写loicalxpath