我使用以下代码来查找包含文本的表格行。
TabName = (driver.findElementByXPath("//div[@id='instrument_quote_source']/div/div/table/tbody/tr[3]/td[1]/span").Text)
结果是外汇。
如果我尝试通过名称FOREX检测行,则它不起作用。我试着用这个:
TabName = driver.findElementByXPath("//div[@id='instrument_quote_source']/div/div/table/tbody/tr[td[contains(text(),'FOREX')]]/td[1]/span").Text
我们如何仅使用名称来识别表中的相同位置。谢谢。 我尝试过很多种组合:(
这是宏:
Sub Test()
Dim PageTimeoutms As Long
Dim ProcessOk As Boolean
Dim TabName As String
PageTimeoutms = 20000
driver.Start "firefox", "http://www.godmode-trader.de/rohstoffe/gold-preis,133979"
driver.Timeouts.ImplicitWait = 3000
driver.Timeouts.Server = PageTimeoutms
ProcessOk = driver.get("http://www.godmode-trader.de/rohstoffe/gold-preis,133979", PageTimeoutms)
driver.findElementByLinkText("alle Börsenplätze anzeigen", PageTimeoutms).Click
'TabName = (driver.findElementByXPath("//div[@id='instrument_quote_source']/div/div/table/tbody/tr[3]/td[1]/span").Text)
TabName = driver.findElementByXPath("//div[@id='instrument_quote_source']/div/div/table/tbody/tr[td[contains(text(),'FOREX')]]/td[1]/span").Text
End Sub
答案 0 :(得分:1)
让细胞形成目标行:
Private Assert As New Assert
Sub Script1()
Dim driver As New FirefoxDriver
driver.Get "http://www.godmode-trader.de/rohstoffe/gold-preis,133979"
driver.FindElementByLinkText("alle Börsenplätze anzeigen").Click
Set row_forex = driver.FindElementsByXPath("//div[@id='instrument_quote_source']//tr[contains(.,'FOREX')]/td")
Debug.Print row_forex(1).Text ' > FOREX
Debug.Print row_forex(2).Text ' > $/Unze
Debug.Print row_forex(3).Text ' > 1.103,64
driver.Quit
End Sub
另一种解决方案是返回所有值,然后使用所需的行:
Sub Script2()
Dim driver As New FirefoxDriver
driver.Get "http://www.godmode-trader.de/rohstoffe/gold-preis,133979"
driver.FindElementByLinkText("alle Börsenplätze anzeigen").Click
Dim values()
values = driver.FindElementByCss("#instrument_quote_source table").AsTable.Data()
For r = 1 To UBound(values)
If values(r, 1) Like "FOREX" Then
Debug.Print values(r, 1) ' > FOREX
Debug.Print values(r, 2) ' > $/Unze
Debug.Print values(r, 3) ' > 1.103,64
Exit For
End If
Next
driver.Quit
End Sub