4 回答

TA貢獻(xiàn)1775條經(jīng)驗 獲得超8個贊
要click()
在該元素上,您必須引發(fā)WebDriverWait?,并且elementToBeClickable()
可以使用以下任一定位器策略:
cssSelector
:new?WebDriverWait(driver,?20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
xpath
:new?WebDriverWait(driver,?20).until(ExpectedConditions.elementToBeClickable(By.xpath("http://a[@class='css-vote-button?pds-vote-button'?and?starts-with(@id,?'pd-vote-button')]/span[text()='Vote']"))).click();
更新
作為替代方案,您可以使用executeScript()
以下方法:
cssSelector
:((JavascriptExecutor)?driver).executeScript("arguments[0].click();",?new?WebDriverWait(driver,?20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))));
xpath
:((JavascriptExecutor)?driver).executeScript("arguments[0].click();",?new?WebDriverWait(driver,?20).until(ExpectedConditions.elementToBeClickable(By.xpath("http://a[@class='css-vote-button?pds-vote-button'?and?starts-with(@id,?'pd-vote-button')]/span[text()='Vote']"))));

TA貢獻(xiàn)1828條經(jīng)驗 獲得超13個贊
您收到ElementClickInterceptedException
錯誤,這意味著頁面上的某些其他元素與您嘗試單擊的元素重疊。您需要以某種方式與頁面進(jìn)行交互,以使重疊元素不再重疊,或者使用 JavaScript 單擊該元素并觸發(fā)“單擊”事件。
許多網(wǎng)站都有與用戶一起滾動的頁面導(dǎo)航元素,因此諸如浮動導(dǎo)航標(biāo)題之類的內(nèi)容可能會阻礙您想要單擊的元素。DebanjanB 有一個很好的解決方案作為解決此問題的下一步,但我懷疑您在等待元素可單擊時會收到 TimeoutException。
您很可能需要觀看此自動化測試的執(zhí)行,然后在測試失敗后查看頁面,然后再發(fā)現(xiàn)如何解決此問題。

TA貢獻(xiàn)1845條經(jīng)驗 獲得超8個贊
為什么不執(zhí)行以下操作來單擊?
WebElement click = driver.findElement(By.id("pd-vote-button10359300")); click.click()

TA貢獻(xiàn)1873條經(jīng)驗 獲得超9個贊
可能您的 ID 正在更改。請嘗試以下 xpath。
//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']
代碼:
WebElement click = driver.findElement(By.xpath("//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
添加回答
舉報