我试图选择一个下拉列表,然后选择其中的值,不带id且类名带有空格。我正在查看的html是
<input class="us-form-input us-form-input us-form-input--blocked autocomplete-input__input us-form-select" type="text" placeholder="Search for address" icon="magnify" value="">
<ul class="us-list--rest address-select__address-list">
<li><button>Address 1</button></li>
<li><button>Address 2</button></li>
<li><button>Address 3</button></li>
<li data-event="true" data-event-category="gas-electricity" data-event-action="supply-address" data-event-label="address-not-listed">
<button>My address is not listed</button>
</li>
</ul>
我是xpath的新手,但认为无法搜索带有空格的类。我尝试通过占位符和下拉列表中的值进行搜索,但没有成功。
请参阅下面的python代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body") != None
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.uswitch.com/gas-electricity/")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
search_form = driver.find_element_by_id('input-postcode')
search_form.send_keys('SW7 2BX')
wait = ui.WebDriverWait(driver, 10)
driver.find_element_by_xpath("//*[contains(text(),'Compare energy deals now')]").click()
wait = ui.WebDriverWait(driver, 20)
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
#driver.find_element_by_css_selector("us-form-input.us-form-input.us-form-input--blocked").click()
driver.find_element_by_xpath("//input[@placeholder='Search for address']").click()
答案 0 :(得分:1)
由于html中存在一个iframe,因此您需要先切换到iframe,然后单击该元素,就可以像这样打开iframe:
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
然后您可以使用xpath单击下拉菜单:
driver.find_element_by_xpath("//input[@placeholder='Search for address']").click()
然后您可以选择以下值:
driver.find_element_by_xpath("//button[text()='Address 1']").click()
与OP讨论后更新答案:
下拉菜单的值是预先显示在UI上的,iframe也不在其html上方,因此您无需单击下拉菜单或切换到iframe,因此只需使用以下代码行它会起作用:
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body") != None
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.uswitch.com/gas-electricity/")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
search_form = driver.find_element_by_id('input-postcode')
search_form.send_keys('SW7 2BX')
wait = ui.WebDriverWait(driver, 10)
driver.find_element_by_xpath("//*[contains(text(),'Compare energy deals now')]").click()
wait = ui.WebDriverWait(driver, 20)
driver.find_element_by_xpath("(//ul[contains(@class,'address-select')]//button)[1]").click()
java中的代码:(与OP讨论之后)
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path to chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.uswitch.com/gas-electricity/");
driver.findElement(By.id("input-postcode")).click();
Thread.sleep(1000);
driver.findElement(By.id("input-postcode")).sendKeys("SW7 2BX");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[contains(text(),'Compare energy deals now')]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//button[text()='Imperial College, Department Of Mechanical Engineering, Exhibition Road, London, SW7 2BX']")).click();
}
答案 1 :(得分:0)