<div class="et_pb_button_module_wrapper et_pb_button_4_tb_header_wrapper et_pb_button_alignment_center et_pb_button_alignment_phone_center et_pb_module" >
<a class="et_pb_button et_pb_button_4_tb_header full-width-btn et_hover_enabled et_pb_bg_layout_light" href="https://pegramonline.com/compare-insurance-companies-charlotte-nc/"> Get a Quote</a>
</div>
我試著定位<a>元素,但還是無法定位。請給我其他建議......類名有一個空格
By.cssSelector("a[class='et_pb_button et_pb_button_4_tb_header full-width-btn et_hover_enabled et_pb_bg_layout_light']");
By.cssSelector("a.et_pb_button.et_pb_button_4_tb_header.full-width-btn.et_hover_enabled.et_pb_bg_layout_light");
By.xpath("/a[contains(@class, 'et_pb_button') and contains(@class, 'et_pb_button_4_tb_header') and contains(@class, 'full-width-btn') and contains(@class, 'et_hover_enabled') and contains(@class, 'et_pb_bg_layout_light')]");
uj5u.com熱心網友回復:
如果你試圖在這個CODE
中放入一個屬性顏色或其他東西,它就能正常作業。a.et_pb_button.et_pb_button_4_tb_header.full-width-tn.et_hover_enabled.et_pb_bg_layout_light{
color:red;
或者
a[class='et_pb_button et_pb_button_4_tb_header full-width-btn et_hover_enabled et_pb_bg_layout_light']{
color:red;
}
那么你就可以很容易地理解,它作業得很好。
uj5u.com熱心網友回復:
在Selenium中,有4種方法可以點擊。
使用下面的xpath
/a[包含(text(),'Get a Quote')]
代碼試驗1 :
Thread.sleep(5);
driver.findElement(By.xpath("/a[包含(text(),'Get a Quote')]").click()。
代碼試用2 :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/a[包含(text(),'Get a Quote')]")).click()。
代碼試用3 :
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("/a[contains(text(), 'Get a Quote')]")。
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button)。
代碼試驗4 :
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("/a[contains(text(), 'Get a Quote')]")。
new Actions(driver).moveToElement(button).click().build().former()。
PS :請在dev tools(Google chrome)中檢查我們在HTML DOM中是否有unique條目。
檢查的步驟:
在Chrome中按F12 -> 進入element部分 -> 做一個CTRL F -> 然后粘貼xpath,看看你想要的element是否被highlighted與1/1匹配節點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330151.html
標籤:
