[英]Unable to click on a radio button in Selenium Webdriver
我正在使用Java學習Selenium Webdriver。 作為一個學習示例,我嘗試打開MakeMyTrip ,訪問“ 國際航班”頁面 ,然后單擊Google Chrome中的“ 單向”單選按鈕。 我嘗試了其他方法來定位此單選按鈕,但仍無法正常工作。 請在下面的代碼示例中找到。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TryRadioClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "Chrome exe path");
WebDriver driver=new ChromeDriver();
driver.get("http://www.makemytrip.com/international-flights");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
boolean displayFlag = driver.findElement(By.linkText("ONE WAY")).isDisplayed();
System.out.println("Display Flag :- "+displayFlag);
boolean enableFlag = driver.findElement(By.linkText("ONE WAY")).isEnabled();
System.out.println("Enable Flag :- "+enableFlag);
if(displayFlag==true && enableFlag==true)
{
WebElement element=driver.findElement(By.linkText("ONE WAY"));
element.click();
System.out.println("Tried to click One Way");
}
}
}
誰能幫我解決這個問題?
使用以下代碼:-
if(displayFlag==true && enableFlag==true)
{
try{
Thread.sleep(5000);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
WebElement element=driver.findElement(By.xpath("//span[@class='radio_state']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
System.out.println("Tried to click One Way");
}
享受..如果仍然遇到任何問題,請與我聯系:)
單擊鏈接有時可能會跳過單選按鈕的檢查。 嘗試直接單擊單選按鈕(或輸入html標記),而不是單擊錨標記。 這是一個例子-
WebElement ele=driver.findElement(By.xpath("//input[@value='one_way']"));
ele.click();
希望這可以幫助。
根據我的檢查,特定的單選按鈕具有以下結構:
<a id="one_way_button1" href="javascript:void(0);" onclick="change_trip_type('one_way_button', 'trip_type', 'o');" class="one_way_button trip_type row first seg_text" tabindex="1">
<span class="radio_state">
<input type="radio" name="way_fields" value="one_way">
</span> ONE WAY
</a>
因此,您要單擊的不是包含“單向”的標簽a ,而是內部的跨度 。 您可以嘗試使用xpath來定位跨度
"//a[text()='ONE WAY']/span[@class='radio_state']"
在xpath下嘗試。
//*[@id="one_way_button1"]/span/input
它應該工作。
driver.findElement(By.xpath(//*[@id=\"one_way_button1\"]/span/input)).click();
過去我們發現使用收音機和單擊會發生上述情況(有時會單擊它,有時卻不會單擊它),並且在我們嘗試避免等待任務/Thread.Sleep時(由於此時定時發出問題,可能會導致在不同的環境下)。 僅此一項就可以在使用thread.sleeps:p時迅速成為一個巨大的頭痛。
我們親自發現,有時最好的解決方案是先發送一個單擊,然后再發送(Sendkeys.Enter或Sendkeys.Space),或者僅發送(Sendkeys.Enter或Sendkeys.Space),不要將點擊與無線電。 特別是在使用Telerik控件的頁面上。 這樣一來,它往往可以在多台機器/環境上工作而無需添加Thread.Sleep,並使使用該步驟的每個測試花費的時間都比需要的時間長(當您有1000個5s thread.sleep即將添加的測試時,請相信我。如果很多測試都使用相同的方法
只是在混合中添加另一種可能的解決方案...
Thread.sleep(time)
Exception
捕獲。 與其嘗試,不如嘗試更安全:
public Boolean isDisplayed() {
try {
wait.withTimeout(20).until(new ExpectedCondition<Boolean>() {
@Nullable @Override public Boolean apply(WebDriver input) {
return videoComponent.isDisplayed();
}
});
} catch (TimeoutException e) {
return false;
}
return true;
}
此代碼將在20秒內檢查標記,直到return true
為止。
如果不是,則20秒后它將return false
。
始終指定要catch
Exception
類型。 在其他情況下,它是無用的。
我嘗試了多種方法來強制硒等待單選按鈕可見,但是它一直在超時。 我最終不得不為點擊標簽而感到滿意:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = driver.findElement(selector);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();
所選位置定義為:
By selector = By.cssSelector(cssSelector);
HTML看起來像這樣:
...
<div class="multi-choice">
<input id="wibble" type="radio" name="wobble" value="TEXT">
<label for="wibble">Wobble</label>
</div>
...
還有一個示例字符串cssSelector:
String cssSelector = "label[for='wibble']"
這是執行此操作的更合適的方法,我剛剛對其進行了測試,並且效果很好。 最佳做法是等待元素可單擊。 您可以將WebDriverWait
與ExpectedConditions
。 這將執行多達20秒鍾的操作,直到元素繼續執行。 與Thread.sleep()
相比,這是一個很大的優勢。
driver.get("http://www.makemytrip.com/international-flights");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement oneWayRadioButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("one_way_button1")));
oneWayRadioButton.click();
System.out.println("Clicked One Way");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.