无法点击一个元素。

huangapple go评论101阅读模式
英文:

Can't click on a element

问题

  1. 尝试访问该网站并单击元素。可以看到,该网站显示一些经济新闻,当单击时,会显示包含信息的图表,这是我的目标——打开图表。由于某种原因,我只能在表格数据(`td[1]`)为1时打开图表(这仅适用于第一个经济新闻)。当表格数据(`td[3]`)变为3(更远未来的经济新闻)时,我无法再打开图表。以下代码有效:

wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[4]/div[5]/table/tbody/tr[13]/td[1]/div/div[1]").click()

  1. 当更改为`td[3]`时无效:

wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[4]/div[5]/table/tbody/tr[93]/td[3]/div/div[1]").click()

  1. 我尝试单击多个不同的元素,但在尝试单击`td[3]`元素时仍无效。
  2. 尝试打开经济新闻的图表,但仅当`td[1]`时有效,对`td[3]`无效。
英文:

Code that i am using:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time
  4. wd = webdriver.Chrome()
  5. url = 'https://www.dailyfx.com/economic-calendar#next-seven-days'
  6. wd.get(url)
  7. time.sleep(20)
  8. try:
  9. wd.find_element(By.XPATH, "/html/body/div[7]/div/div/button/img").click()
  10. except:
  11. print('No Calendar Advertisement')
  12. try:
  13. wd.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div/div[2]/button").click()
  14. except:
  15. print('No Cookies Button')
  16. time.sleep(3)
  17. try:
  18. wd.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/div/div[1]/span").click()
  19. except:
  20. print('No App Advertisement')
  21. #Clear calendar filter
  22. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[1]/div[2]").click()
  23. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/label").click()
  24. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[2]/label").click()
  25. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[3]/label").click()
  26. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[4]/label").click()
  27. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[5]/label").click()
  28. #Selecting only United States
  29. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[1]/div[1]/div/span").click()
  30. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/div/div/div[1]/label").click()
  31. #Closing Calendar Filter
  32. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[3]/div/div[1]/div[1]/div[2]").click()
  33. #Working part:
  34. wd.find_element(By.XPATH,"/html/body/div[5]/div/div[4]/div[2]/div/div/div[1]/div/div/div[4]/div[5]/table/tbody/tr[13]/td[1]/div/div[1]").click()

https://www.dailyfx.com/economic-calendar#next-seven-days

So, I am accessing this website and trying to click on a element. As you can see, the website shows some economic news and when you click on it, it shows a graphic with information, which is my goal - opening the graph. For some reason, I can only open the graphic when the table data (td\[1\]) is 1(that occurs just for the first economic news). When the table data(td\[3\]) change to 3(economic news that are more distant to happen), I can't open the graphic anymore. This code works:

  1. wd.find_element(By.XPATH,"/html/body/div\[5\]/div/div\[4\]/div\[2\]/div/div/div\[1\]/div/div/div\[4\]/div\[5\]/table/tbody/tr\[13\]/td\[1\]/div/div\[1\]").click() When change to td\[3\], doesnt work: wd.find_element(By.XPATH,"/html/body/div\[5\]/div/div\[4\]/div\[2\]/div/div/div\[1\]/div/div/div\[4\]/div\[5\]/table/tbody/tr\[93\]/td\[3\]/div/div\[1\]").click()

I tried clicking on multiple different elements, but still doesn't work when trying to click on td\[3\] elements.

Tried to open a graphic of a economic news but only work when td\[1\], not for td\[3\].

答案1

得分: 1

尝试这段代码(查找元素,滚动到它,休眠,点击),对我有效:

  1. element = driver.find_element(By.XPATH, "//table/tbody/tr[13]/td[3]")
  2. driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', element)
  3. time.sleep(1)
  4. element.click()

如果仍然存在问题,请尝试以下替代方法来执行 element.click()

  1. # 替代方法1
  2. driver.execute_script("arguments[0].click()", element)
  3. # 替代方法2
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium.webdriver.common.keys import Keys
  6. actions = ActionChains(driver)
  7. element.send_keys(Keys.RETURN)
  8. # 替代方法3
  9. actions.move_to_element(element)
  10. actions.click(element).perform()
  11. # 替代方法4
  12. actions.move_to_element(element).click().perform()
  13. # 替代方法5
  14. actions.move_to_element(element).send_keys(Keys.RETURN).perform()
  15. # 替代方法6
  16. actions.click_and_hold(element).perform()
  17. time.sleep(1)
  18. actions.release(element).perform()
英文:

Try this code (find element, scroll to it, sleep, click), works for me

  1. element = driver.find_element(By.XPATH, "//table/tbody/tr[13]/td[3]")
  2. driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', element)
  3. time.sleep(1)
  4. element.click()

If you are still having problems try these alternatives to element.click()

  1. # alternative 1
  2. driver.execute_script("arguments[0].click()", element)
  3. # alternative 2
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. from selenium.webdriver.common.keys import Keys
  6. actions = ActionChains(driver)
  7. element.send_keys(Keys.RETURN)
  8. # alternative 3
  9. actions.move_to_element(element)
  10. actions.click(element).perform()
  11. # alternative 4
  12. actions.move_to_element(element).click().perform()
  13. # alternative 5
  14. actions.move_to_element(element).send_keys(Keys.RETURN).perform()
  15. # alternative 6
  16. actions.click_and_hold(element).perform()
  17. time.sleep(1)
  18. actions.release(element).perform()

huangapple
  • 本文由 发表于 2023年2月18日 00:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定