Form1.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using _Log;
  2. using System;
  3. using System.Windows.Forms;
  4. namespace _09_B_WebBrowser
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. log = new Log(textBox1);
  12. }
  13. Form2 f2;
  14. WebBrowser wb;
  15. Log log;
  16. private void btnOpen_Click(object sender, EventArgs e)
  17. {
  18. if (f2 == null)
  19. {
  20. f2 = new Form2();
  21. wb = f2.webBrowser1;
  22. f2.FormClosed += F2_FormClosed;
  23. wb.DocumentCompleted += Wb_DocumentCompleted;
  24. f2.Show();
  25. }
  26. }
  27. private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  28. {
  29. log.l($"Wb_DocumentCompleted: {e.Url}");
  30. }
  31. private void F2_FormClosed(object sender, FormClosedEventArgs e)
  32. {
  33. f2 = null;
  34. wb = null;
  35. }
  36. private void btnQuit_Click(object sender, EventArgs e)
  37. {
  38. f2.Close();
  39. f2 = null;
  40. wb = null;
  41. }
  42. private void btnNavigate_Click(object sender, EventArgs e)
  43. {
  44. wb.Navigate("https://www.google.pl");
  45. }
  46. private void btnLogoSrc_Click(object sender, EventArgs e)
  47. {
  48. HtmlElement img = wb.Document.GetElementById("hplogo");
  49. img.SetAttribute("src", "http://212.87.228.200:3000/img/favicon.png");
  50. }
  51. private void btnLogoRemove_Click(object sender, EventArgs e)
  52. {
  53. HtmlElement img = wb.Document.GetElementById("hplogo");
  54. img.OuterHtml = "";
  55. }
  56. private void btnInput_Click(object sender, EventArgs e)
  57. {
  58. HtmlElementCollection coll = wb.Document.GetElementsByTagName("input");
  59. foreach (var o in coll)
  60. {
  61. HtmlElement el = o as HtmlElement;
  62. if (el.Name == "q")
  63. {
  64. el.SetAttribute("value", "Ala ma kota");
  65. el.Focus();
  66. break;
  67. }
  68. }
  69. }
  70. private void btnClick_Click(object sender, EventArgs e)
  71. {
  72. HtmlElementCollection coll = wb.Document.GetElementsByTagName("input");
  73. foreach (var o in coll)
  74. {
  75. HtmlElement el = o as HtmlElement;
  76. if (el.Name == "btnG")
  77. {
  78. el.Focus();
  79. el.InvokeMember("Click");
  80. break;
  81. }
  82. }
  83. }
  84. private void btnAppendChild_Click(object sender, EventArgs e)
  85. {
  86. HtmlElement h1 = wb.Document.CreateElement("h1");
  87. h1.InnerHtml = "WITAJCIE!!!";
  88. HtmlElementCollection coll = wb.Document.GetElementsByTagName("input");
  89. foreach (var o in coll)
  90. {
  91. HtmlElement el = o as HtmlElement;
  92. if (el.Name == "btnG")
  93. {
  94. el.Parent.AppendChild(h1);
  95. break;
  96. }
  97. }
  98. }
  99. private void btnNavigate2_Click(object sender, EventArgs e)
  100. {
  101. wb.Navigate("http://212.87.228.200:3000/user/login");
  102. }
  103. private void btnInputs_Click(object sender, EventArgs e)
  104. {
  105. wb.Document.GetElementById("user_name").SetAttribute("value", "student");
  106. wb.Document.GetElementById("password").SetAttribute("value", "123456");
  107. }
  108. private void btnClick2_Click(object sender, EventArgs e)
  109. {
  110. HtmlElementCollection coll = wb.Document.GetElementsByTagName("button");
  111. foreach (var o in coll)
  112. {
  113. HtmlElement el = o as HtmlElement;
  114. if (el.OuterHtml.Contains("ui green button"))
  115. {
  116. el.InvokeMember("Click");
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. }