2017-06-06 99 views
1

我在嘗試使用我的瀏覽器自動發佈評論。對於我提出這個代碼使用xpath和硒設置facebook註釋

static void Facebook() 
    { 
     var url = "http://facebook.com"; 

     var email = "email"; 
     var password = "password"; 

     ChromeOptions options = new ChromeOptions(); 
     options.AddArgument("--disable-notifications"); 


     driver = new ChromeDriver(options); 

     driver.Url = url; 

     driver.Manage().Window.Maximize(); 

     driver.FindElement(By.Id("email")).SendKeys(email); 
     driver.FindElement(By.Id("pass")).SendKeys(password + Keys.Enter); 



     driver.Url = "https://www.facebook.com/CsharpCorner/photos/a.487296564614322.115393.194086953935286/1572378532772781/?type=3&theater"; 

     driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); 

     driver.FindElement(By.XPath(@"/html[@id='facebook']/body[@class='hasLeftCol _2yq home composerExpanded _5vb_ fbx _-kb s_v05quun15 chrome webkit win x1 Locale_en_US']/div[@id='photos_snowlift']/div[@class='_n9']/div[@class='_n3']/div[@class='fbPhotoSnowliftContainer snowliftPayloadRoot uiContextualLayerParent']/div[@class='clearfix fbPhotoSnowliftPopup']/div[@class='rhc photoUfiContainer']/div[@id='fbPhotoSnowliftDetails']/form[@id='u_4b_5']/div[@id='fbPhotoSnowliftFeedbackInput']/ul[@id='u_jsonp_3_18']/div[@id='addComment_1572378532772781']/div[@class='UFIMentionsInputWrap UFIStickersEnabledInput clearfix']/div[2]/div[@class='UFIImageBlockContent _42ef _8u']/div[@class='UFICommentContainer']/div[@class='_fmi UFIInputContainer']/div[@class='_2xwx _289c']/div[@id='composer_text_input_box']/div[@class='_5yk2']/div[@class='_5rp7']/div[@class='_5rpb']/div[@class='notranslate _5rpu']/div/div/div[@class='_1mf _1mj']")) 
      .SendKeys("123123"); 

     Thread.Sleep(2000); 
     driver.Quit(); 

    } 

這workd完美,直到driver.findElement我已經複製使用xpath helper chrome extension但可惜的是我得到錯誤

沒有這樣的元素的XPath:無法找到元素:。 ..

我該怎麼辦?

+0

我們需要使用...動態xpath ....你能給我們DOM –

+0

@santhoshkumar我有問題的網址。請參閱'driver.Url' – gsiradze

+0

我已經登錄並點擊了網址。我得到了該帖子的放大視圖,我們是否需要點擊右側的「評論」按鈕進行評論? –

回答

1

我懷疑你的XPath是過於具體,這裏是我會做什麼:

首先,建立一個XPath時,通過尋找一個id開始,然後找「組織」屬性,如classnamevalue, etc ...

對於您的情況,由於您使用的是自動生成XPath的工具,因此您應該嘗試簡化它,查找與上述相同的規則。

這將使它更容易調試,同時使XPath更具通用性。這可以解決您的工具在生成XPath時過於具體的情況,例如,某個元素的class是由某些JavaScript動態生成的。


這裏是我的過程中,使用的XPath:

/html[@id='facebook']/body[@class='hasLeftCol _2yq home composerExpanded _5vb_ fbx _-kb s_v05quun15 chrome webkit win x1 Locale_en_US']/div[@id='photos_snowlift']/div[@class='_n9']/div[@class='_n3']/div[@class='fbPhotoSnowliftContainer snowliftPayloadRoot uiContextualLayerParent']/div[@class='clearfix fbPhotoSnowliftPopup']/div[@class='rhc photoUfiContainer']/div[@id='fbPhotoSnowliftDetails']/form[@id='u_4b_5']/div[@id='fbPhotoSnowliftFeedbackInput']/ul[@id='u_jsonp_3_18']/div[@id='addComment_1572378532772781']/div[@class='UFIMentionsInputWrap UFIStickersEnabledInput clearfix']/div[2]/div[@class='UFIImageBlockContent _42ef _8u']/div[@class='UFICommentContainer']/div[@class='_fmi UFIInputContainer']/div[@class='_2xwx _289c']/div[@id='composer_text_input_box']/div[@class='_5yk2']/div[@class='_5rp7']/div[@class='_5rpb']/div[@class='notranslate _5rpu']/div/div/div[@class='_1mf _1mj'] 

開始從右到左,與id尋找一個節點,並刪除其左側的所有節點:

/div[@id='composer_text_input_box']/div[@class='_5yk2']/div[@class='_5rp7']/div[@class='_5rpb']/div[@class='notranslate _5rpu']/div/div/div[@class='_1mf _1mj'] 

再次從右到左開始,查找具有屬性的節點,並刪除其左側的所有節點(id節點除外):

/div[@id='composer_text_input_box']/div[@class='_1mf _1mj'] 

id節點和屬性節點之前添加///表示在DOM樹上遞歸地匹配節點。

//div[@id='composer_text_input_box']//div[@class='_1mf _1mj'] 

最後,通過對XPath的搜索(CTRL +˚F)驗證在瀏覽器的控制檯(F12)的正確性。

0

我可以使用Selenium在Facebook上成功添加評論。

下面是Java代碼:

String url = "http://facebook.com"; 

    String email = "email"; 
    String password = "password"; 
    System.setProperty("webdriver.chrome.driver", "src/chromedriver 3"); 
    WebDriver driver = new ChromeDriver(); 

    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("--disable-notifications"); 


    driver = new ChromeDriver(options); 

    driver.get(url); 

    driver.manage().window().maximize(); 

    driver.findElement(By.id("email")).sendKeys("Your email ID"); 
    driver.findElement(By.id("pass")).sendKeys("Your password" + Keys.ENTER); 
    driver.get("https://www.facebook.com/CsharpCorner/photos/a.487296564614322.115393.194086953935286/1572378532772781/?type=3&theater"); 



    Thread.sleep(1000); 

    WebElement element_comment= driver.findElement(By.xpath("//ul[@class='UFIList']/div[contains(@id,'addComment')]//div[@class='UFICommentContainer']//div[contains(@class,'UFIAddCommentInput')]")); 

    element_comment.click(); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//div[@contenteditable='true']")).sendKeys("Hello World"); 

注:

  • 你已經使用了點擊的元素的路徑不能用於元素髮送鍵,反之亦然。
  • 有一個div,屬性爲contenteditable =「true」。點擊評論框後,您將能夠在HTML中看到這個div。