簡體   English   中英

在硒中定位href元素

[英]Locating href element in Selenium

下面是我的代碼

<TD ALIGN="LEFT" width="65%" id="oldcontent" >3. Download csv template
    <a href="javascript:doformsubmit('3');"><IMG src="images/date_pick.gif" border=0></a>
</TD>

如何找到具有javascript函數調用且沒有id的href元素。感謝您的幫助

我在回答我自己的問題:以下語法有效

assertTrue(selenium.isElementPresent("//a[@href=\"javascript:doformsubmit('3');\"]"));

您可以使用BeautifulSoup來讀取網頁的源代碼。

以下解決方案是在python中完成的,但是您應該能夠在java中使用類似的方法。 編輯:除了BeautifulSoup不適用於Java :) =>您應該切換到python。

soup = BeautifulSoup(br.page_source)
tmp = soup.findAll('a')
for t in tmp:
     string =  t['href']

然后,您可以使用正則表達式來捕獲每個具有javascript的href。

您可以使用適當的Xpath表達式(例如// a [(@ href ='javascript:doformsubmit('3');')]),請查看此文章以獲取更多示例。

這樣做的方法是-

driver.findElement(By.linkText(“ javascript:doformsubmit('3');”));

如果您的意圖類似於單擊href,則可以實際使用TD,因為它具有ID,因此我可以很容易地找到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM