How to use dom_attribute method of Selenium.WebDriver Package

Best Selenium code snippet using Selenium.WebDriver.dom_attribute

element_spec.rb

Source:element_spec.rb Github

copy

Full Screen

...70 before { driver.navigate.to url_for('formPage.html') }71 context 'string type' do72 let(:element) { driver.find_element(id: 'checky') }73 let(:prop_or_attr) { 'type' }74 it '#dom_attribute returns attribute value' do75 expect(element.dom_attribute(prop_or_attr)).to eq 'checkbox'76 end77 it '#property returns property value' do78 expect(element.property(prop_or_attr)).to eq 'checkbox'79 end80 it '#attribute returns value' do81 expect(element.attribute(prop_or_attr)).to eq 'checkbox'82 end83 end84 context 'numeric type' do85 let(:element) { driver.find_element(id: 'withText') }86 let(:prop_or_attr) { 'rows' }87 it '#dom_attribute String' do88 expect(element.dom_attribute(prop_or_attr)).to eq '5'89 end90 it '#property returns Number' do91 expect(element.property(prop_or_attr)).to eq 592 end93 it '#attribute returns String' do94 expect(element.attribute(prop_or_attr)).to eq '5'95 end96 end97 context 'boolean type of true' do98 let(:element) { driver.find_element(id: 'checkedchecky') }99 let(:prop_or_attr) { 'checked' }100 it '#dom_attribute returns String', except: {browser: :safari} do101 expect(element.dom_attribute(prop_or_attr)).to eq 'true'102 end103 it '#property returns true' do104 expect(element.property(prop_or_attr)).to eq true105 end106 it '#attribute returns String' do107 expect(element.attribute(prop_or_attr)).to eq 'true'108 end109 it '#dom_attribute does not update after click', except: {browser: :safari} do110 element.click111 expect(element.dom_attribute(prop_or_attr)).to eq 'true'112 end113 it '#property updates to false after click' do114 element.click115 expect(element.property(prop_or_attr)).to eq false116 end117 it '#attribute updates to nil after click' do118 element.click119 expect(element.attribute(prop_or_attr)).to eq nil120 end121 end122 context 'boolean type of false' do123 let(:element) { driver.find_element(id: 'checky') }124 let(:prop_or_attr) { 'checked' }125 it '#dom_attribute returns nil' do126 expect(element.dom_attribute(prop_or_attr)).to be_nil127 end128 it '#property returns false' do129 expect(element.property(prop_or_attr)).to eq false130 end131 it '#attribute returns nil' do132 expect(element.attribute(prop_or_attr)).to be_nil133 end134 it '#dom_attribute does not update after click' do135 element.click136 expect(element.dom_attribute(prop_or_attr)).to eq nil137 end138 it '#property updates to true after click' do139 element.click140 expect(element.property(prop_or_attr)).to eq true141 end142 it '#attribute updates to String after click' do143 element.click144 expect(element.attribute(prop_or_attr)).to eq 'true'145 end146 end147 context 'property exists but attribute does not' do148 let(:element) { driver.find_element(id: 'withText') }149 let(:prop_or_attr) { 'value' }150 it '#dom_attribute returns nil' do151 expect(element.dom_attribute(prop_or_attr)).to be_nil152 end153 it '#property returns default property' do154 expect(element.property(prop_or_attr)).to eq 'Example text'155 end156 it '#attribute returns default property' do157 expect(element.attribute(prop_or_attr)).to eq 'Example text'158 end159 it '#property returns updated property' do160 element.clear161 expect(element.property(prop_or_attr)).to be_empty162 end163 it '#attribute returns updated property' do164 element.clear165 expect(element.attribute(prop_or_attr)).to be_empty166 end167 end168 context 'attribute exists but property does not' do169 let(:element) { driver.find_element(id: 'vsearchGadget') }170 let(:prop_or_attr) { 'accesskey' }171 it '#dom_attribute returns attribute' do172 expect(element.dom_attribute(prop_or_attr)).to eq '4'173 end174 it '#property returns nil' do175 expect(element.property(prop_or_attr)).to be_nil176 end177 it '#attribute returns attribute' do178 expect(element.attribute(prop_or_attr)).to eq '4'179 end180 end181 context 'neither attribute nor property exists' do182 let(:element) { driver.find_element(id: 'checky') }183 let(:prop_or_attr) { 'nonexistent' }184 it '#dom_attribute returns nil' do185 expect(element.dom_attribute(prop_or_attr)).to be_nil186 end187 it '#property returns nil' do188 expect(element.property(prop_or_attr)).to be_nil189 end190 it '#attribute returns nil' do191 expect(element.attribute(prop_or_attr)).to be_nil192 end193 end194 context 'style' do195 before { driver.navigate.to url_for('clickEventPage.html') }196 let(:element) { driver.find_element(id: 'result') }197 let(:prop_or_attr) { 'style' }198 it '#dom_attribute attribute with no formatting' do199 expect(element.dom_attribute(prop_or_attr)).to eq 'width:300;height:60'200 end201 # TODO: This might not be correct behavior202 it '#property returns object',203 except: [{browser: :firefox,204 reason: 'https://github.com/mozilla/geckodriver/issues/1846'},205 {browser: :safari}] do206 expect(element.property(prop_or_attr)).to eq %w[width height]207 end208 it '#attribute returns attribute with formatting' do209 expect(element.attribute(prop_or_attr)).to eq 'width: 300px; height: 60px;'210 end211 end212 context 'incorrect casing' do213 let(:element) { driver.find_element(id: 'checky') }214 let(:prop_or_attr) { 'nAme' }215 it '#dom_attribute returns correctly cased attribute' do216 expect(element.dom_attribute(prop_or_attr)).to eq 'checky'217 end218 it '#property returns nil' do219 expect(element.property(prop_or_attr)).to be_nil220 end221 it '#attribute returns correctly cased attribute' do222 expect(element.attribute(prop_or_attr)).to eq 'checky'223 end224 end225 context 'property attribute case difference with attribute casing' do226 let(:element) { driver.find_element(name: 'readonly') }227 let(:prop_or_attr) { 'readonly' }228 it '#dom_attribute returns a String', except: {browser: :safari} do229 expect(element.dom_attribute(prop_or_attr)).to eq 'true'230 end231 it '#property returns nil' do232 expect(element.property(prop_or_attr)).to be_nil233 end234 it '#attribute returns a String' do235 expect(element.attribute(prop_or_attr)).to eq 'true'236 end237 end238 context 'property attribute case difference with property casing' do239 let(:element) { driver.find_element(name: 'readonly') }240 let(:prop_or_attr) { 'readOnly' }241 it '#dom_attribute returns a String',242 except: [{browser: :firefox,243 reason: 'https://github.com/mozilla/geckodriver/issues/1850'},244 {browser: :safari}] do245 expect(element.dom_attribute(prop_or_attr)).to eq 'true'246 end247 it '#property returns property as true' do248 expect(element.property(prop_or_attr)).to eq true249 end250 it '#attribute returns property as String' do251 expect(element.attribute(prop_or_attr)).to eq 'true'252 end253 end254 context 'property attribute name difference with attribute naming' do255 let(:element) { driver.find_element(id: 'wallace') }256 let(:prop_or_attr) { 'class' }257 it '#dom_attribute returns attribute value' do258 expect(element.dom_attribute(prop_or_attr)).to eq 'gromit'259 end260 it '#property returns nil' do261 expect(element.property(prop_or_attr)).to be_nil262 end263 it '#attribute returns attribute value' do264 expect(element.attribute(prop_or_attr)).to eq 'gromit'265 end266 end267 context 'property attribute name difference with property naming' do268 let(:element) { driver.find_element(id: 'wallace') }269 let(:prop_or_attr) { 'className' }270 it '#dom_attribute returns nil' do271 expect(element.dom_attribute(prop_or_attr)).to be_nil272 end273 it '#property returns property value' do274 expect(element.property(prop_or_attr)).to eq 'gromit'275 end276 it '#attribute returns property value' do277 expect(element.attribute(prop_or_attr)).to eq 'gromit'278 end279 end280 context 'property attribute value difference' do281 let(:element) { driver.find_element(tag_name: 'form') }282 let(:prop_or_attr) { 'action' }283 it '#dom_attribute returns attribute value' do284 expect(element.dom_attribute(prop_or_attr)).to eq 'resultPage.html'285 end286 it '#property returns property value' do287 expect(element.property(prop_or_attr)).to match(%r{http://(.+)/resultPage\.html})288 end289 it '#attribute returns property value' do290 expect(element.attribute(prop_or_attr)).to match(%r{http://(.+)/resultPage\.html})291 end292 end293 end294 it 'returns ARIA role', only: {browser: %i[chrome edge]} do295 driver.navigate.to "data:text/html," \296 "<div role='heading' aria-level='1'>Level 1 Header</div>" \297 "<h1>Level 1 Header</h1>" \298 "<h2 role='alert'>Level 2 Header</h2>"...

Full Screen

Full Screen

element.rb

Source:element.rb Github

copy

Full Screen

...81 #82 # This method attempts to provide the most likely desired current value for the attribute83 # of the element, even when that desired value is actually a JavaScript property.84 # It is implemented with a custom JavaScript atom. To obtain the exact value of the attribute or property,85 # use #dom_attribute or #property methods respectively.86 #87 # More exactly, this method will return the value of the property with the given name,88 # if it exists. If it does not, then the value of the attribute with the given name is returned.89 # If neither exists, null is returned.90 #91 # The "style" attribute is converted as best can be to a text representation with a trailing semi-colon.92 #93 # The following are deemed to be "boolean" attributes, and will return either "true" or "false":94 #95 # async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked,96 # defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate,97 # iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate,98 # nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking,99 # selected, spellcheck, truespeed, willvalidate100 #101 # Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:102 #103 # When the value of "class" is requested, the "className" property is returned.104 # When the value of "readonly" is requested, the "readOnly" property is returned.105 #106 # @param [String] name attribute name107 # @return [String, nil] attribute value108 #109 # @see #dom_attribute110 # @see #property111 #112 def attribute(name)113 bridge.element_attribute self, name114 end115 #116 # Gets the value of a declared HTML attribute of this element.117 #118 # As opposed to the #attribute method, this method119 # only returns attributes declared in the element's HTML markup.120 #121 # If the attribute is not set, nil is returned.122 #123 # @param [String] name attribute name124 # @return [String, nil] attribute value125 #126 # @see #attribute127 # @see #property128 #129 def dom_attribute(name)130 bridge.element_dom_attribute @id, name131 end132 #133 # Gets the value of a JavaScript property of this element134 # This will return the current value,135 # even if this has been modified after the page has been loaded.136 # If the value is not set, nil is returned.137 #138 # @param [String] name property name139 # @return [String, nil] property value140 #141 def property(name)142 bridge.element_property @id, name143 end144 #...

Full Screen

Full Screen

scraper.rb

Source:scraper.rb Github

copy

Full Screen

...12 @word = driver.find_element(:css, ".js-wotd-wordsound-plus").text13 @kana = driver.find_element(:css, ".js-wotd-word-transliterated").text14 @translation = driver.find_element(:css, ".js-wotd-translation").text15 @part_of_speach = driver.find_element(:css, ".js-wotd-wordtype").text16 @word_audio_url = driver.find_element(:css, ".native-word").find_element(:xpath, ".//*[local-name()='source'][1]").dom_attribute("src")17 @example_sentence = driver.find_element(:css, ".js-wotd-phrasesound-plus").text18 @example_sentence_kana = driver.find_element(:css, ".js-wotd-phrase-transliterated").text19 @example_sentence_translation = driver.find_element(:css, ".js-wotd-enphrase").text20 @sentence_audio_url = driver.find_element(:css, ".native-example").find_element(:xpath, ".//*[local-name()='source'][1]").dom_attribute("src")21 self22 end23 private24 def chromedriver_path25 @chromedriver_path ||= begin26 return "bin/chromedriver" if File.exist?("bin/chromedriver")27 return "/usr/local/bin/chromedriver" if File.exist?("/usr/local/bin/chromedriver")28 raise "Sorry, I couldn't find the chromedriver binanary"29 end30 end31end...

Full Screen

Full Screen

dom_attribute

Using AI Code Generation

copy

Full Screen

1puts driver.find_element(:name, "q").attribute("title")2puts browser.text_field(:name, "q").attribute_value("title")3puts driver.find_element(:name, "q").attribute("title")4puts driver.find_element(:name, "q").attribute("title")5puts browser.text_field(:name, "q").attribute_value("title")6puts browser.text_field(:name, "q").attribute_value("title")7puts browser.text_field(:name, "q").attribute_value("title")8puts browser.text_field(:name, "q").attribute_value("title")

Full Screen

Full Screen

dom_attribute

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:id, 'myid')2puts element.attribute('class')3element = driver.find_element(:id, 'myid')4puts element.attribute('class')5element = driver.find_element(:id, 'myid')6puts element.attribute('class')7element = driver.find_element(:id, 'myid')8puts element.attribute('class')9element = driver.find_element(:id, 'myid')10puts element.attribute('class')11element = driver.find_element(:id, 'myid')12puts element.attribute('class')

Full Screen

Full Screen

dom_attribute

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:id, 'myid')2puts element.attribute('class')3element = driver.find_element(:id, 'myid')4puts element.attribute('class')5element = driver.find_element(:id, 'myid')6puts element.attribute('class')7element = driver.find_element(:id, 'myid')8puts element.attribute('class')9element = driver.find_element(:id, 'myid')10puts element.attribute('class')11element = driver.find_element(:id, 'myid')12puts element.attribute('class')

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful