英文:
Can't click on a element
问题
尝试访问该网站并单击元素。可以看到,该网站显示一些经济新闻,当单击时,会显示包含信息的图表,这是我的目标——打开图表。由于某种原因,我只能在表格数据(`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()
当更改为`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()
我尝试单击多个不同的元素,但在尝试单击`td[3]`元素时仍无效。
尝试打开经济新闻的图表,但仅当`td[1]`时有效,对`td[3]`无效。
英文:
Code that i am using:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
wd = webdriver.Chrome()
url = 'https://www.dailyfx.com/economic-calendar#next-seven-days'
wd.get(url)
time.sleep(20)
try:
wd.find_element(By.XPATH, "/html/body/div[7]/div/div/button/img").click()
except:
print('No Calendar Advertisement')
try:
wd.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div/div[2]/button").click()
except:
print('No Cookies Button')
time.sleep(3)
try:
wd.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/div/div[1]/span").click()
except:
print('No App Advertisement')
#Clear calendar filter
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()
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()
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()
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()
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()
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()
#Selecting only United States
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()
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()
#Closing Calendar Filter
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()
#Working part:
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:
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
尝试这段代码(查找元素,滚动到它,休眠,点击),对我有效:
element = driver.find_element(By.XPATH, "//table/tbody/tr[13]/td[3]")
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', element)
time.sleep(1)
element.click()
如果仍然存在问题,请尝试以下替代方法来执行 element.click()
:
# 替代方法1
driver.execute_script("arguments[0].click()", element)
# 替代方法2
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
actions = ActionChains(driver)
element.send_keys(Keys.RETURN)
# 替代方法3
actions.move_to_element(element)
actions.click(element).perform()
# 替代方法4
actions.move_to_element(element).click().perform()
# 替代方法5
actions.move_to_element(element).send_keys(Keys.RETURN).perform()
# 替代方法6
actions.click_and_hold(element).perform()
time.sleep(1)
actions.release(element).perform()
英文:
Try this code (find element, scroll to it, sleep, click), works for me
element = driver.find_element(By.XPATH, "//table/tbody/tr[13]/td[3]")
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', element)
time.sleep(1)
element.click()
If you are still having problems try these alternatives to element.click()
# alternative 1
driver.execute_script("arguments[0].click()", element)
# alternative 2
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
actions = ActionChains(driver)
element.send_keys(Keys.RETURN)
# alternative 3
actions.move_to_element(element)
actions.click(element).perform()
# alternative 4
actions.move_to_element(element).click().perform()
# alternative 5
actions.move_to_element(element).send_keys(Keys.RETURN).perform()
# alternative 6
actions.click_and_hold(element).perform()
time.sleep(1)
actions.release(element).perform()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论