使用Selenium,点击JS作为href link



我希望关于如何使用超链接获得硒的一些澄清或建议。我试过用几乎所有可能的元素来选择它。我还附上了一个图像与源查看和比较…

html示例:


<div class="x-grid3-cell-inner x-grid3-col-ACTION_COLUMN" id="5005a00001rJ22q_ACTION_COLUMN"><a href="javascript:srcUp(%27%2F5005a00001rJ22q%2Fe%3FretURL%3D%252F500%253Ffcf%253D00B5a00000AtOBk%2526rolodexIndex%253D-1%2526page%253D1%2526isdtp%253Dnv%26isdtp%3Dvw%27);"><span>Edit</span></a> | <a href="javascript: void(0);" title="Follow this case to receive updates in your feed." entityid="5005a00001rJ22q" class="chatterFollowUnfollowAction " revtitle="Stop following this case to stop receiving updates in your feed."></a></div>

例子:

#clicky = driver.find_element_by_xpath('//a[contains(@href,"javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);")]').click()
#clicky = driver.find_elements_by_partial_link_text('javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);').click()
#clicky = driver.find_element_by_xpath("//a[@href='javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);']").click()

试试这个:

driver.find_element_by_xpath("//div[.//a[contains(@class,'chatterFollowUnfollowAction')]]//a[contains(@href,'javascript:srcUp')]").click()

要在访问该元素之前显式地添加等待时间,请使用this:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[.//a[contains(@class,'chatterFollowUnfollowAction')]]//a[contains(@href,'javascript:srcUp')]"))).click()

试试这个:

someElement = driver.find_element_by_xpath("//a[contains(@class, 'chatterFollowUnfollowAction' and @title,'Follow this case')]")
driver.execute_script("arguments[0].click();",someElement)

最新更新