Reputation: 4062
I have an HTML with a div tag which has an ID attribute. The ID value is dynamic. I have managed to build an XPATH to locate the ID attribute and get's it's value into a variable. Can i print out the value to the console? I want to know what value the variable has got.
I tried to print the value the following but i get errors: I tried:
print id.text
print id.get_attribute("id")
The errors are:
AttributeError: 'unicode' object has no attribute 'get_attribute'
AttributeError: 'unicode' object has no attribute 'text'
The Selenium Python code is:
element = self.driver.find_element(By.XPATH, '//*[starts-with(@id,"operations_add_process_list_task")]//span//button')
id = element.get_attribute("id")
print id.text
The XPATH is:
(By.XPATH, '//*[starts-with(@id,"operations_add_process_list_task")]//span//button')
The HTML is:
<div id="operations_add_process_list_task_2">
<span/>
<span>
<span class="myinlineblock" title="Clean"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;">
<select tabindex="-1">
</span>
</span>
<span>
<span class="" title="Turn group off or on." style="">
<input type="checkbox" checked="" tabindex="-1"/>
</span>
</span>
<span>
<button class="gwt-Button" title="Add the tasks to the selected group." style="display:block;" type="button">Add tasks
</button>
</span>
</div>
Once I know what the value is I can then use the ID for the next locator e.g.
select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="%s"]/span[2]//select' % id))))
Thanks, Riaz
Upvotes: 0
Views: 2339
Reputation: 89295
Alternatively, you can change the XPath to be as shown below to return the element with id
that also contains the button
element :
//*[starts-with(@id,"operations_add_process_list_task")][.//span//button]
Or you might want to use child
axes instead of //
if the span is direct child of the element with the target id
, because the former will be slightly more efficient :
//*[starts-with(@id,"operations_add_process_list_task")][span/button]
Upvotes: 1