2016-05-02 24 views
0

我想點擊文字鏈接,不工作:硒 - 如何可靠地點擊文本?還沒完全解決

from selenium import webdriver 
driver = webdriver.Firefox() 
driver.get("https://my.gumtree.com/login") 
driver.find_element_by_name("username").send_keys(email) 
driver.find_element_by_id("existingUser").click() 
driver.find_element_by_id("fld-password").send_keys(password) 
driver.find_element_by_xpath("//*[contains(text(), 'Continue')]").click() # works 

driver.get("https://my.gumtree.com/postad") 
driver.find_elements_by_xpath("//*[text()='For Sale']")[-1].click() # this now works, thanks 
# driver.find_element_by_xpath("//*[contains(@span,'Appliances']").click() # this worked but I need the next line instead 
driver.find_element_by_xpath("//span[text()[normalize-space()='Phones, Mobile Phones & Telecoms']]").click() # this does not work 
driver.find_element_by_xpath("//span[text()[normalize-space()='Phones, Mobile Phones & Telecoms']]").click() # this does not work either 
driver.find_element_by_xpath("//span[text()[normalize-space()='Mobile Phones']]").click() 
driver.find_element_by_xpath("//span[text()[normalize-space()='Other']]").click() 

HTML:

<li class="border-b is-parent" data-category-name="Appliances" data-category-url="kitchen-appliances" data-category-id="2485" data-category-children="true"> 
    <span class="category-name"> 
    <span class="category-list-control is-parent"> 
    </span> 
    Appliances 
    </span> 
</li> 

我該怎麼辦錯了嗎?

文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字

+0

任何一個鏈接的HTML代碼? –

+0

<跨度類= 「BTN-縮進-ICN ICN-標籤TXT-孤兒從 - 米」> ::之前 出售 Castle

+0

嘗試這樣//跨度[含有(文本(),「對於銷售')] –

回答

1

硒中的XPath發現這是一個有點打破了不指定標籤名稱元素的遞歸搜索。所以不要使用://*[]

此外XPath有點壞。在您的示例中,您正在搜索具有名爲「span」的屬性的元素,該屬性包含任何字符串。

它應該是這樣的:

如果你願意,你可以嘗試germanium,一個免費的測試框架,我寫的,與這種矛盾和更多的交易。在鍺中,相同的代碼如下所示: click(Text("Other Home Appliances"))

+0

它適用於第一項'待售',謝謝。但它不適用於其他人:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{「method」:「xpath」,「selector」:「// a [contains(string(。),' Appliances')]「} – Castle

+0

您嘗試使用A標籤選擇LI元素:)。 嘗試:'// li [contains(string(),'Appliances')]' 或鍺:'Element(「li」,contains_text =「Appliances」)。element()' –

+0

最後一個「銷售」在Germanium中有一個使用JavaScript進行搜索的選擇器,所以你可以直接編寫:Text(「Appliances」)。element()'而不必關心元素類型。 –

1

嘗試下面的代碼。希望能幫助到你。

driver.find_element_by_xpath("//*[text()='For Sale']") 
+0

它的第一個項目'出售'工作,謝謝。但它不適用於其他人:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{「method」:「xpath」,「selector」:「// * [text()='Appliances'] 「} – Castle

1

按提供的HTML代碼,我希望它會工作

//span[contains(text(),'For Sale')] 

感謝, 穆拉利

+0

它工作的第一個項目'待售',謝謝。但它不適用於其他人:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{「method」:「xpath」,「selector」:「// span [contains(text(),'Appliances ')]「} – Castle

0

您提供HTML不表明家電跨度包含了很多空白的這很可能是你沒有找到這個元素的原因。嘗試下面的XPath找到家電跨度,

//span[text()[normalize-space()='Appliances']] 

下面是我使用的腳本,

from selenium import webdriver 

email, password = 'your-own-email', 'your-own-password' 
driver = webdriver.Firefox() 
driver.implicitly_wait(10) 

driver.get("https://my.gumtree.com/login") 
driver.find_element_by_name("username").send_keys(email) 
driver.find_element_by_id("existingUser").click() 
driver.find_element_by_id("fld-password").send_keys(password) 
driver.find_element_by_xpath("//*[contains(text(), 'Continue')]").click() 

driver.get("https://my.gumtree.com/postad") 
driver.find_elements_by_xpath("//*[text()='For Sale']")[-1].click() 
driver.find_element_by_xpath("//span[text()[normalize-space()='Appliances']]").click() 
driver.find_element_by_xpath("//span[text()[normalize-space()='Home Appliances']]").click() 
driver.find_element_by_xpath("//span[text()[normalize-space()='Other Home Appliances']]").click() 

對於那些未在視圖中使用下面的方法元素,

element = driver.find_element_by_xpath("//span[text()[normalize-space()='Phones, Mobile Phones & Telecoms']]") 
driver.execute_script("return arguments[0].scrollIntoView();", element) 
element.click() 
+0

不,它不工作我很害怕:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{」method「:」xpath「,」selector「:」// span [text ()[normalize-space()='Appliances']]「} – Castle

+0

@Castle我添加了我使用的腳本,這個腳本正在爲我工​​作。請注意,在這種情況下,'implicitly_wait()'調用也可能很重要! – sowa

+0

Try'// li [contains(string(),'Appliances')]' –

0

如果您在HTML代碼中有固定字符串,則無需在xpath中使用方法。請使用XPath的text()方法:

driver.findElement(By.xpath(.//*[text()='Appliances'])).click(); 

使用這個,如果你的HTML代碼中包含單詞「型消」兩個或多個元素的原因,它不會選擇任何元素。所以,爲了避免模棱兩可,我們可以直接使用text()=

希望這會有所幫助。

+0

不,它不工作我很害怕:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{「method」:「xpath」,「selector」:「.//* [text()='電器']「} – Castle

0

這種包含XPath中可以在高級場景中使用,比如你:

//span[text()[contains(.,'Appliances')]] 

@Edit:

您可以測試這個主題谷歌在所有的XPath響應Chrome使用$x("selector")。也許問題出在你的硒版本。

我真的不知道Python語法(我曾經在C#中創建測試),但是你可以做一個醜陋的代碼,它可以工作。嘗試將代碼更改爲python,我認爲存在一些錯誤:

elements = driver.find_elements_by_cssselector("span.category-name") 

for element in elements: 
    if (element.text == "Appliances" || element.text == "Home Appliances" || element.text == "Other Home Appliances") 
     element.click() 
+0

不,它不工作我很害怕:selenium.common.exceptions.NoSuchElementException:消息:無法找到元素:{」method「:」xpath「,」selector「:」// span [text ()[contains(。''Appliances')]]「} – Castle

+0

@Castle現在有一個使用python工作的代碼示例。我認爲在語法上有一些錯誤,因爲我不習慣用這種語言編程。嘗試一下 –