<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; VBA: Доступ к элементу div страницы на html посредством VBA]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17703&amp;type=atom" />
	<updated>2023-03-28T06:48:26Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=17703</id>
		<entry>
			<title type="html"><![CDATA[Re: VBA: Доступ к элементу div страницы на html посредством VBA]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=157394#p157394" />
			<content type="html"><![CDATA[<p><strong>AleksP</strong>,</p><div class="codebox"><pre><code>
Dim sUrl, oHttpRequest, oDocument, oElement
sUrl = &quot;https://synapsenet.ru/zakupki/fz223/32312204305%231--sanktpeterburg-vipolnenie-rabot-po-obsledovaniyu-stroitelnih&quot;
Set oHttpRequest = CreateObject(&quot;WinHttp.WinHttpRequest.5.1&quot;)
With oHttpRequest
	.Open &quot;GET&quot;, sUrl, false
	.Send
	Set oDocument = CreateObject(&quot;htmlfile&quot;)
	With oDocument
		&#039; Более безопасный парсинг тела документа
		.designMode = &quot;on&quot;
		.open
		.write oHttpRequest.ResponseText
		.close
	End With
	For Each oElement in oDocument.all.tags(&quot;div&quot;)
		If InStr(1,oElement.className,&quot;tct-tender-text&quot;,vbTextCompare) &gt; 0 Then
			MsgBox oElement.innerText
			Exit For
		End if
	Next
End With
</code></pre></div><p>Как альтернатива, если на машине <span class="bbu">точно</span> установлен IE выше семёрки, можно инициализировать документ в режиме более высокой версии и тогда будут доступны методы <strong>querySelectorAll</strong> и <strong>getElementsByClassName</strong></p>]]></content>
			<author>
				<name><![CDATA[Xameleon]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=8836</uri>
			</author>
			<updated>2023-03-28T06:48:26Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=157394#p157394</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: VBA: Доступ к элементу div страницы на html посредством VBA]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=157254#p157254" />
			<content type="html"><![CDATA[<p><strong>AleksP</strong>, добро пожаловать на форум! Ознакомьтесь, пожалуйста, с <a href="https://forum.script-coding.com/misc.php?action=rules">Правилами</a>, отредактируйте свой пост. Обратите внимание на оформление <a href="https://forum.script-coding.com/misc.php?action=rules#code-formatting">кода</a>.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2023-03-19T13:18:00Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=157254#p157254</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[VBA: Доступ к элементу div страницы на html посредством VBA]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=157240#p157240" />
			<content type="html"><![CDATA[<p>Добрый день!</p><p>В html коде есть элемент div, к которому необходимо получить доступ посредством VBA<br /></p><div class="codebox"><pre><code>
   &lt;div id=&quot;tender-card-content&quot;&gt;
                    &lt;div id=&quot;report-script&quot; class=&quot;tender-card-title&quot;&gt;
                        &lt;h1 class=&quot;tct-tender-number&quot;&gt;
                            Закупка &lt;span class=&quot;copy-script tender-number-copy&quot;&gt;32312204305&lt;/span&gt;
                        &lt;/h1&gt;
                        &lt;div class=&quot;tct-tender-text&quot;&gt;[b]Содержимое, которое необходимо скопировать с html страницы через VBA[/b]&lt;/div&gt;
...</code></pre></div><p>Получается только через getElementsByTagName(&quot;div&quot;)(номер элемента).innerText, но номер элемента div может меняться динамически и такой вариант не подходит.<br />Привожу код, в котором попробовал разные варианты доступа с указанием сообщения о ошибке в комментариях.</p><div class="codebox"><pre><code>
Sub test()

sURL = &quot;https://synapsenet.ru/zakupki/fz223/32312204305%231--sanktpeterburg-vipolnenie-rabot-po-obsledovaniyu-stroitelnih&quot;

    Set oXMLHTTP = CreateObject(&quot;MSXML2.XMLHTTP&quot;)
    With oXMLHTTP
        .Open &quot;GET&quot;, sURL, False
        .send
        txt = .responseText
        
        Set pDoc = CreateObject(&quot;HTMLFile&quot;)
        pDoc.body.innerHTML = txt
   
       .Open &quot;GET&quot;, sURL, False: .send
        getHTTP = StrConv(.responseBody, vbUnicode)
        
        MsgBox pDoc.getElementsByTagName(&quot;div&quot;)(номер элемента).innerText
        &#039;Работает.&#039;

        MsgBox getHTTP.getElementsByTagName(&quot;div&quot;)(номер элемента).innerText
        &#039;Сообщение об ошибке Object required.&#039;
        
        pDoc.getElementsByClassName (&quot;tct-tender-text&quot;)
        &#039;Сообщение об ошибке Object doesn&#039;t support this property or method.&#039;
        
       getHTTP.getElementsByClassName (&quot;tct-tender-text&quot;)
        &#039;Сообщение об ошибке Object required.&#039;
   
       pDoc.querySelectorAll (&quot;tct-tender-text&quot;)
      &#039;Сообщение об ошибке Object doesn&#039;t support this property or method.&#039;
      
        getHTTP.querySelectorAll (&quot;tct-tender-text&quot;)
        &#039;Сообщение об ошибке Object required.&#039;

    End With
    
    Set oXMLHTTP = Nothing

End Sub</code></pre></div><p>Как нужно исправить код, чтобы получить доступ к нужному элементу?</p>]]></content>
			<author>
				<name><![CDATA[AleksP]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=43070</uri>
			</author>
			<updated>2023-03-19T11:15:48Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=157240#p157240</id>
		</entry>
</feed>
